Running Sitecore in a Docker container is easier than you might think

Sitecore in a Docker container

Motivation

Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries — anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.

https://www.docker.com/what-docker

I was looking forward to Docker support of Windows containers for a long time. Docker supports Windows containers nowadays and it means we are able to put our Windows applications in containers in order to save a lot of time spent on setting up development, testing, and deployment.

These days Microsoft gives a lot of Dockerfile examples so setting up a container for ASP.NET web application is not a pain but a pleasure.

In this article, I am going to show how to perform a very basic setup for Sitecore Experience Platform 8.2.

To set it up, I mixed the following Dockerfiles:

Setup

There are many options how to install Sitecore Experience Platform. For the very basic demo setup, we will need the following:

  • ZIP archive of the Sitecore site root folder.
  • A Sitecore license (I hope you have got one).
  • Microsoft SQL Server installer. You can get it from here (Microsoft SQL Express 2016).
  • You’ll need to run Windows 10 or Windows Server 2016 on your PC or laptop.

Once you have all these, we can go further.

Docker setup

Follow this article to setup Docker on Windows. If you already have Docker installed, please, verify the version of dockerd service. I have spent some time trying to figure out that the version of the dockerd service on my machine is not the same as I just installed. Quick check – you should be able to pull Windows containers like that:

docker pull microsoft/windowsservercore

And here are the first step to start building a Sitecore container:

  • Create a folder for our container (i.e. C:\docker).
  • Create a Dockerfile in the folder we just created.
  • Start our Dockerfile by specifying the Base Image for our container:

FROM microsoft/windowsservercore

Files

I put Sitecore files just in the same way as for local quick setup. The structure is like that:

docker
  sitecore
    Databases
      ...
    Website
      ...
      Data
        license.xml
      ...

I will not dive deep into Sitecore configuration. You just need to place files as you usually do when you setup your Sitecore instances. Remember about ConnectionStrings.config and a path to license.xml. Don’t attach the databases.

Add to our Dockerfile the following lines to make the files available in the container:

COPY . /
WORKDIR /

Microsoft SQL

Now we need to set up Microsoft SQL Server. Add to the Dockerfile a following line:

RUN SQLEXPR_x64_ENU.exe /qs /x:setup && /setup/setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT="NT AUTHORITY\System" /SQLSYSADMINACCOUNTS="BUILTIN\ADMINISTRATORS" /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS && del /F /Q SQLEXPR_x64_ENU.exe && rd /q /s setup

Remember, SQLEXPR_x64_ENU.exe must be at the root of our folder, like: C:\docker\SQLEXPR_x64_ENU.exe.

Next, I run a PowerShell script to enable sa account, change the password and attach databases. Add the following line to our Dockerfile:

RUN powershell ./sql.ps1 -Verbose

Here is sql.ps1:

IIS

In the same way as for Microsoft SQL Server, setup IIS:

RUN powershell ./iis.ps1 -Verbose

Here is iis.ps1:

Build

Now we have all we need to build an image. Here’s our final Dockerfile:

To build an image, let’s create a build.bat file in our folder (C:\docker) with following content:

docker build -t sitecore %~dp0

Run this file and wait for the build to complete. Then, if we run docker images we shall see the sitecoreimage. Now let’s use it!

Create a container and run it

To create containers from our image run a command like this:

docker run --name sitecore -p 80:80 -it sitecore powershell

This will log you into powershell console on your Docker container. Run ping sitecore to find an IP-address of our Sitecore instance:

Pinging 172.26.102.196 with 32 bytes of data

Add to C:\Windows\System32\drivers\etc\hosts file a following line:

172.26.102.196 sitecore

Open http://sitecore/ in a browser, it should look like this:

Sitecore in a Docker container

This is it! In the same way, we can setup Mongo and Solr.