How to Run Old Versions of Solr in a Docker Container

> Please don't make me install another version of Solr on my local...
Cover Image for How to Run Old Versions of Solr in a Docker Container

The Scenario

Let's say that you have a production site in Azure running Solr on a virtual machine. The indexes are located in C:\Solr\solr-x.x.x\server\solr. You want to get this running on your local machine without having to install yet another version of Solr, so you decide to use Docker. Nothing fancy... No SolrCloud, no HTTPS. Just a simple install of Solr x.x.x running in a Docker container.

Getting Started

In the upstream C:\Solr\solr-x.x.x\server\solr directory you might have these folders:


_4
- sitecore_master_index
_4
- sitecore_web_index
_4
- sitecore_core_index
_4
- client_custom_index

Zip up all the index folders. You don't need to include the data folders within each index folder, as they will be created by Solr when it starts up. But in my experience, you can.

While being mindful of potential egress fees from where you are hosting the zip file, copy the zip to your local machine. Unzip it wherever you like. Perhaps you might even want to unzip it within your project directory and add the core.properties file and the conf folder to source control?

Now let's set up the Docker configuration.

Docker Compose

Create a file called docker-compose.yml in the root of your project:


_15
version: '3.6'
_15
_15
services:
_15
solr:
_15
build:
_15
context: .\docker\build\solr
_15
image: solr
_15
scale: 0
_15
hostname: "solr"
_15
ports:
_15
# TODO: update this port as desired. Maybe write something fancy like a init.ps1 that populates a .env with your specific directory path and consume it here...
_15
- "8983:8983"
_15
volumes:
_15
# TODO: update this path as needed
_15
- C:/projects/My-Project/solr:C:/solr/server/solr

Docker File

Create a directory called /docker/build/solr and create a file called Dockerfile within it:


_37
# escape=`
_37
_37
FROM mcr.microsoft.com/powershell:nanoserver-1809
_37
_37
# Install Amazon Corretto (Java)
_37
RUN curl.exe -L -o jre.zip https://corretto.aws/downloads/latest/amazon-corretto-8-x64-windows-jre.zip && `
_37
mkdir C:\jre && `
_37
tar.exe -xf jre.zip -C C:\jre --strip-components=1 && `
_37
del jre.zip
_37
_37
USER ContainerAdministrator
_37
RUN SETX /M JAVA_HOME "C:\jre" && `
_37
SETX /M PATH "%PATH%;%JAVA_HOME%\bin"
_37
USER ContainerUser
_37
_37
# Install Solr
_37
# TODO: replace your version of Solr here
_37
RUN curl.exe -L -o solr.zip https://archive.apache.org/dist/lucene/solr/x.x.x/solr-x.x.x.zip && `
_37
mkdir C:\solr && `
_37
tar.exe -xf solr.zip -C C:\solr --strip-components=1 && `
_37
del solr.zip
_37
_37
# Solr configuration
_37
COPY ./solr.in.cmd C:\solr\bin\
_37
_37
# TODO: consume port from .env
_37
EXPOSE 8983
_37
_37
HEALTHCHECK CMD powershell -command `
_37
try { `
_37
$response = iwr http://localhost:8983; `
_37
if ($response.StatusCode -eq 200) { return 0} `
_37
else {return 1}; `
_37
} catch { return 1 }
_37
_37
COPY ./start.cmd /
_37
ENTRYPOINT ["start.cmd"]

Solr Configuration Files

Create a file called solr.in.cmd and place it in the same directory as your Dockerfile:


_21
@echo off
_21
_21
REM Increase Java Min/Max Heap as needed to support your indexing / query needs
_21
set SOLR_JAVA_MEM=-Xms1g -Xmx1g
_21
_21
REM Enables HTTPS. It is implicitly true if you set SOLR_SSL_KEY_STORE. Use this config
_21
REM to enable https module with custom jetty configuration.
_21
set SOLR_SSL_ENABLED=false
_21
_21
REM Require clients to authenticate
_21
set SOLR_SSL_NEED_CLIENT_AUTH=false
_21
_21
REM Enable clients to authenticate (but not require)
_21
set SOLR_SSL_WANT_CLIENT_AUTH=false
_21
_21
REM Verify client hostname during SSL handshake
_21
set SOLR_SSL_CLIENT_HOSTNAME_VERIFICATION=false
_21
_21
REM SSL Certificates contain host/ip "peer name" information that is validated by default. Setting
_21
REM this to false can be useful to disable these checks when re-using a certificate on many hosts
_21
set SOLR_SSL_CHECK_PEER_NAME=false

Create a file called start.cmd and place it in the same directory as your Dockerfile. Here's what it will look like:


_2
REM Start Solr in foreground
_2
C:\solr\bin\solr start -p 8983 -f

up.ps1

As a little bit of extra fanciness, we can start to make this idiot proof and do a check to ensure that the 8983 port isn't already listening with a process on your host machine. If it is, we can prompt the user to kill it. This is useful if you have multiple projects running on your local machine and you don't want to have to remember which one is using the port.

From there, we build and run the container, and then open the Solr admin site.

Create a file called up.ps1 in the root of your project:


_29
$process = Get-NetTCPConnection -LocalPort 8983 -State Listen -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | Get-Process
_29
_29
if ($process) {
_29
Write-Host "Process $($process.ProcessName) with ID $($process.Id) is using the port that the solr container needs: 8983"
_29
_29
# Prompt the user for confirmation
_29
$confirmation = Read-Host "Do you want to kill this process? (y/n)"
_29
_29
if ($confirmation -eq 'y') {
_29
# Kill the process
_29
Stop-Process -Id $process.Id -Force
_29
Write-Host "Process $($process.ProcessName) with ID $($process.Id) has been killed." -ForegroundColor Green
_29
}
_29
else {
_29
Write-Host "No action taken."
_29
}
_29
}
_29
else {
_29
Write-Host "No service is running on port: 8983. Continuing..." -ForegroundColor Green
_29
}
_29
_29
docker compose build
_29
docker compose up -d
_29
_29
Write-Host "Done! Remember to populate solr managed schema and reindex if needed." -ForegroundColor Green
_29
_29
Write-Host "Opening solr admin site..." -ForegroundColor Green
_29
Start-Sleep -Seconds 10
_29
Start-Process http://localhost:8983/solr/#/

Build and Run

From the root of your project, run .\up.ps1.

When the Solr admin site loads up, you should see your cores listed. You might need to wait a while for Solr to initialize, so if the interface looks broken or some cores are missing, just wait a bit and refresh. You can also check the logs by running docker compose logs -f from the root of your project.

Final Steps

Update your ConnectionStrings.config like so:


_1
<add name="solr.search" connectionString="http://localhost:8983/solr" />

From there, populate the Solr managed schema and reindex if needed.

Keep going FAST,

MG


More Stories

Cover Image for Add TypeScript Type Checks to RouteData fields

Add TypeScript Type Checks to RouteData fields

> Inspired by error: Conversion of type may be a mistake because neither type sufficiently overlaps with the other.

Cover Image for Tips for Forms Implementations

Tips for Forms Implementations

> And other pro tips

Cover Image for SPE Script Performance & Troubleshooting

SPE Script Performance & Troubleshooting

> Script never ends or runs too slow? Get in here.

Cover Image for On Sitecore Development

On Sitecore Development

> Broadly speaking

Cover Image for On Mentorship and Community Contributions

On Mentorship and Community Contributions

> Reflections and what I learned as an MVP mentor

Cover Image for Year in Review: 2022

Year in Review: 2022

> Full steam ahead

Cover Image for Don't Ignore the HttpRequestValidationException

Don't Ignore the HttpRequestValidationException

> Doing so could be... potentially dangerous

Cover Image for JSS + TypeScript Sitecore Project Tips

JSS + TypeScript Sitecore Project Tips

> New tech, new challenges

Cover Image for Troubleshooting 502 Responses in Azure App Services

Troubleshooting 502 Responses in Azure App Services

> App Services don't support all libraries

Cover Image for Super Fast Project Builds with Visual Studio Publish

Super Fast Project Builds with Visual Studio Publish

> For when solution builds take too long

Cover Image for Symposium 2022 Reflections

Symposium 2022 Reflections

> Sitecore is making big changes

Cover Image for Sitecore Symposium 2022

Sitecore Symposium 2022

> What I'm Watching 👀

Cover Image for NextJS/JSS Edit Frames Before JSS v21.1.0

NextJS/JSS Edit Frames Before JSS v21.1.0

> It is possible. We have the technology.

Cover Image for NextJS: Short URL for Viewing Layout Service Response

NextJS: Short URL for Viewing Layout Service Response

> Because the default URL is 2long4me

Cover Image for Security Series: App Service IP Restrictions

Security Series: App Service IP Restrictions

> How to manage IP rules "at scale" using the Azure CLI

Cover Image for Content Editor Search Bar Not Working

Content Editor Search Bar Not Working

> Sometimes it works, sometimes not

Cover Image for NextJS: Access has been blocked by CORS policy

NextJS: Access has been blocked by CORS policy

> CORS is almost as much of a nuisance as GDPR popups

Cover Image for Early Returns in React Components

Early Returns in React Components

> When and how should you return early in a React component?

Cover Image for NextJS: Unable to Verify the First Certificate

NextJS: Unable to Verify the First Certificate

> UNABLE_TO_VERIFY_LEAF_SIGNATURE

Cover Image for Script: Boost SIF Certificate Expiry Days

Script: Boost SIF Certificate Expiry Days

> One simple script that definitely won't delete your system32 folder

Cover Image for JSS: Reducing Bloat in Multilist Field Serialization

JSS: Reducing Bloat in Multilist Field Serialization

> Because: performance, security, and error-avoidance

Cover Image for Critical Security Bulletin SC2024-001-619349 Announced

Critical Security Bulletin SC2024-001-619349 Announced

> And other scintillating commentary

Cover Image for On Sitecore Stack Exchange (SSE)

On Sitecore Stack Exchange (SSE)

> What I've learned, what I see, what I want to see

Cover Image for Hello World

Hello World

> Welcome to the show

Cover Image for Ideas For Docker up.ps1 Scripts

Ideas For Docker up.ps1 Scripts

> Because Docker can be brittle

Cover Image for Azure PaaS Cache Optimization

Azure PaaS Cache Optimization

> App Services benefit greatly from proper configuration

Cover Image for Tips for Applying Cumulative Sitecore XM/XP Patches and Hotfixes

Tips for Applying Cumulative Sitecore XM/XP Patches and Hotfixes

> It's probably time to overhaul your processes

Cover Image for Tips for New Sitecore Developers

Tips for New Sitecore Developers

> If I had more time, I would have written a shorter letter