Sitecore demo in a Docker Container

Posted 4 June 2018 12:00 AM by Benjamin Moles, Senior Developer @ ClearPeople

A Sitecore 8 instance requires a set of services working together, such as IIS, SQL Server and MongoDB. To prepare a demo website, these services need to be available, so our installation must ensure its presence and resolve any name conflicts with other databases or existing websites. The tool Sitecore Instance Manager can help you to install a Sitecore instance, although it still leaves you with a lot of manual work to do to install the customisations. 

I was wondering if Docker could help. It would be good to have a Docker image with a Sitecore demo already installed with all its dependencies, so it’s only necessary to instantiate a container to have a demo ready for the action. This way you don’t need to install or maintain software on the sales’ team laptop or on a cloud server. You only need a command to create the container with all that’s necessary. In this blog I will tell you more about this.  

What is Docker and why should I use it? 

There are loads of websites introducing Docker, so I won’t go into details. If you need help with it, you can search for “Introduction to Docker” on Google to find plenty of information. 

Explained in a high-level, Docker is a virtualisation system, similar to Hyper-V or VMWare. It has several benefits, but considering the purpose of this blog post, I would like to highlight the following benefits: 

  • It needs less disk space. The Docker containers need less disk space than traditional virtual machines. This is because various containers are sharing the same kernel, while traditional virtual machines have their own operating system (kernel). 
  • Because of other advantages being omitted in this article, Docker can be suitable for incorporating into modern development processes and software deployments (also called DevOps). The use of this technology for commercial demos is an easy way to get in contact with Docker and start using it. If it is already being used in our company, then this would be a new area/sector 

Description of my Sitecore Demo 

The necessary software to support a Sitecore site depends on the Sitecore version and the characteristics and installed modules.

The Sitecore site used in this article is based on Sitecore 8 with the module Email Experience Manager (EXM) and with Experience Database (XDB) enables with a local MongoDB instance. The Search provider used is Lucene.  

Creating a Docker image for my Sitecore demo 

A Docker container is always being created from an image. A developer clould see it as instantiating an object (container) from its class (image). So, the first thing to do will be a Docker image with everything necessary for the demo. 

Necessary elements 

These are the elements that are necessary to create a Docker image with a Sitecore website.  

  • Machine with Docker 
  • Work folder 
  • File “Dockerfile” 
  • Sitecore files and databases 
  • Installation packages: 
    • SQL Server Express x64 
    • Microsoft Visual C++ 2012 Redistributable 
  • Folder with MongoDB files and configuration 
  • PowerShell scripts to install or enable the following prerequisites: 
    • SQL Server 
    • IIS 
    • MongoDB 
    • Microsoft Visual C++ 2012 Redistributable: required for Sitecore EXM. Only necessary if the Sitecore demo includes the “Email Experience Manager” module.

Below we will go through the details of each of these elements.  

Machine with Docker 

To create an image and then execute the containers, we need Docker. In this we use a machine with Windows 10 with “Docker for Windows”, although you can also use Windows Server 2016 and Apple or Linux.  

Docker needs to be configurated in “Windows containers” mode, this allows us to create Windows containers.  

Work folder 

All the necessary for the website being created needs to be in one single folder. The name or address to this is irrelevant as it won’t be referenced from its interior. You can rename and move this in the future. In this case I have created it in:  

C:\docker\scdemo\ 

All the folders and files discussed below will be saved in this folder which we consider to be from scratch.  

“Dockerfile” 

The file “Dockerfile” (without extension) establishes the steps to follow by Docker to create the desired image. It must be created within the work folder at the root level. 

FROM microsoft/windowsservercore 

LABEL Description="Sitecore 8.2 rev. 160729" Vendor="Sitecore" Version="8.2 rev. 160729" 

# Adding the files 
COPY . / 
WORKDIR / 

# Install dependencies 
RUN vcredist_x64.exe /passive /norestart 

# Installing MS SQL Server 
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 

# Setting up MS SQL Server and attaching databases 
RUN powershell ./sql.ps1 -Verbose 

# Setting up IIS and adding the website 
RUN powershell ./iis.ps1 -Verbose 

# Setting up MongoDB 
RUN powershell ./mongo.ps1 

EXPOSE 80 


First of all you need to copy all the content into the root folder within the principal unit of the container (or image in this case) and establish the destination path as work folder. Next, all the necessary software is being installed using in some case the installation packages provided by the provider or using PowerShell scripts. 

In the next section we will see all the used elements for this Docker file. 

Sitecore files and databases 

The easiest way to install a Sitecore site with its corresponding personalisation is using the database and files from an environment where it is already working. It could be the same development environment where the corresponding personalisations (layouts, sublayouts, etc.) have been done. That’s what we are doing here.  

We create a folder in the root level called “sitecore”. In my case it’s: 
“C:\docker\scdemo\sitecore 

  • Data: Will contain the content from the “Data” folder from the site coming from the source/original environment. You can empty all the folders except for the “indexes” in this way we will keep the Lucene indexes while reducing the size of the image and Docker containers. The “Data” folder is the place where, by default, the Sitecore licence file ("license.xml") is being saved. A valid license file must be used. 
  • Databases: Will contain the SQL Services databases used by the website. In my case they are the following (with its corresponding log files): 
    • Sitecore.Core.mdf 
    • Sitecore.Exm.Master.mdf 
    • Sitecore.Exm.Web.mdf 
    • Sitecore.Master.mdf 
    • Sitecore.Reporting.mdf 
    • Sitecore.Web.mdf 
  • Website: Will contain the website files. It can be obtained from the folder indicated by the IIS server of the source environment. 

Installation packages 

Two of the requirements of our Sitecore site are installed through redistributable packages, such as: 

SQL Server Express x64: You can download it here. This package is installed by the file "dockerfile" with the 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 

Microsoft Visual C++ 2012 Redistributable: You can download it here. The line, inside the file "dockerfile" responsible for its installation is: 

RUN vcredist_x64.exe /passive /norestart 

Folder with MongoDB files and configuration 

The installation and administration details of MongoDB are beyond the scope of this document. To simplify things, you can copy files from a MongoDB instance of a development environment, where sharing, replication, or any advanced MongoDB features are not usually applied. We will need the "bin" folder with the MongoDB binaries, the "data" folder with the databases, and if there is a "config" with a configuration file that, being optional, facilitates the task of indicating to MongoDB where you can find the folders of data and logs, along with other details such as the storage engine etc. 

A detail to take into account in the configuration of MongoDB is that the complete path to the necessary folders, inside the container will be different to the path where they are saved on the machine where the Docker image will be created. For example, in my development environment the folder with the MongoDB databases is in "c:\docker\scdemo\MongoDB\3.4\data\dbs", instead, in the MongoDB configuration file (used within the container) the path will be "c: /MongoDB/3.4/data/dbs". As we can see, the root directory where the container image is created ("c:\docker\scdemo\") becomes the root path ("c:/") inside the container. 

An example from a MongoDB configuration file would be the following: 

storage:  
  dbPath: "c:/MongoDB/3.4/data/dbs" 
  # Stores each DB within a folder (not default) 
  directoryPerDB: true 

  # Storage Engine: 
  # Memory Map: 
  #engine: mmapv1 
  # Wired Tiger: It 
  engine: wiredTiger 
systemLog: 
  destination: "file" 
  path: "c:/MongoDB/3.4/data/logs/mongod.log" 
  # Log files management: 
  # Rename existing log file and create a new one 
  logRotate: rename 
  # Append to existing log file 
  #logAppend: true 
  #logRotate: reopen

PowerShell scripts to install or enable prerequisites  

The file “dockerfile”, is supported by the following PowerShell scripts for the tasks described below. 

  • “sql.ps1”: Configure SQL Server to ensure access to Sitecore and attach the necessary databases: 

Set-StrictMode -Version latest ; 
Stop-Service MSSQL`$SQLEXPRESS ; 
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL
Server\MSSQL14.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll' -Name TcpDynamicPorts -Value '0'

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL
Server\MSSQL14.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll' -Name TcpPort -Value '1433' ; 
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL
Server\MSSQL14.SQLEXPRESS\MSSQLServer' -Name LoginMode -Value 2 ; 
Start-Service MSSQL`$SQLEXPRESS ; 
Invoke-Sqlcmd -Query "ALTER LOGIN sa with password='saPassword'; ALTER LOGIN sa ENABLE;" -
ServerInstance ".\SQLEXPRESS" ; 
Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_core]       ON (FILENAME =
'C:\sitecore\Databases\Sitecore.core.mdf'),       (FILENAME =
'C:\sitecore\Databases\demo.local_core_log.ldf')       FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_exm.master] ON (FILENAME =
'C:\sitecore\Databases\Sitecore.exm.master.mdf'), (FILENAME =
'C:\sitecore\Databases\demo.local_exm.master_log.ldf') FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_exm.web]    ON (FILENAME =
'C:\sitecore\Databases\Sitecore.exm.web.mdf'),    (FILENAME =
'C:\sitecore\Databases\demo.local_exm.web_log.ldf')    FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_master]     ON (FILENAME =
'C:\sitecore\Databases\Sitecore.master.mdf'),     (FILENAME =
'C:\sitecore\Databases\demo.local_master_log.ldf')     FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_reporting]  ON (FILENAME =
'C:\sitecore\Databases\Sitecore.reporting.mdf'),  (FILENAME =
'C:\sitecore\Databases\demo.local_reporting_log.ldf')  FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

Invoke-Sqlcmd -Query "CREATE DATABASE [demo.local_web]        ON (FILENAME =
'C:\sitecore\Databases\Sitecore.web.mdf'),        (FILENAME =
'C:\sitecore\Databases\demo.local_web_log.ldf')        FOR ATTACH ;" -ServerInstance ".\SQLEXPRESS"

  • “iis.ps1”: Create and configure the demo website within Internet Information Services (IIS):Add-WindowsFeature Web-Server 

Add-WindowsFeature NET-Framework-45-ASPNET 
Add-WindowsFeature Web-Asp-Net45 
C:\Windows\System32\inetsrv\appcmd.exe delete site "default web site" 
C:\Windows\System32\inetsrv\appcmd.exe add site /name:Sitecore /bindings:http/*:80:
/physicalPath:c:\sitecore\Website 
# Site Bindings 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader demo.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader habitat.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader legal.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader retail.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader finance.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader storefront.demo.local -IPAddress "*" 
New-WebBinding -name Sitecore -port 80 -Protocol http -HostHeader utilities.demo.local -IPAddress
"*" 
C:\Windows\System32\inetsrv\appcmd.exe start site Sitecore 
iisreset 

  • “mongo.ps1”: Configure the Windows service that will run MongoDB: 

c:\MongoDB\3.4\bin\mongod.exe --service --config=c:\MongoDB\3.4\config\mongod.cfg --install 
Get-Service | Where-Object {$_.displayName -eq "MongoDb"} | Start-Service 

Docker image creation process 

Once the necessary elements are prepared, the creation of the image consists of executing the following command: 

docker build -t sitecore %~dp0 

Instance a Docker container with my Sitecore Demo 

Once we have our Docker image, create a container with the demo ready to be used, it is as simple as executing the following command: 

docker run --name sitecore -p 80:80 -it -m 8g sitecore powershell 

Once the container is executed, we will access it through its IP. The following PowerShell command can be used to obtain this IP: 

docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' sitecore 

The version of Docker for Windows 10 has certain limitations that prevent access to the Sitecore site in the container through a more friendly domain (such as "localhost"). More information can be found here. Fortunately, the Docker version for Windows Server 2016 solves this limitation. 

References 

The following articles where very useful while developing this Docker solution. 

  • https://medium.com/@romaklimenko/running-sitecore-in-a-docker-container-is-easier-than-you-might-think-298050b507ff  
  • https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/

More information about Docker to come next week in my new blog!

Share:

Add your comment

 
 

 

Archive

Tagcloud

intranet Modern SharePoint teamwork employee engagement digital workspace SharePoint JavaScript Windows Azure Digital Transformation staff satisfaction productivity Microsoft Teams Office 365 Yammer cms content management system agile GDPR Microsoft Graph collaboration Microsoft sharepoint 2016 upgrade migration SharePoint Online 2016 Tech Trends Digital Disruption Context marketing marketing SharePoint 2010 SharePoint 2013 TFS Git security kentico Analytics jquery QA Quality Assurance testing content management websites Sitecore sitecore marketplace sitecore module cloud Microsoft Cloud Storage digital strategy technical consulting sitecore modules Experience database Sitecore 7 Sitecore 8 support account management customer experience Data Storage cms integration front end front end development prototype Cloud Storage StorSimple Front-end Development Layout SharePoint 2013 colour palette UI design website design log viewer sitecore cms website Azure big data business-critical sharepoint accessibility android apple chrome clear people clearpeople debug emulator ios mobile testing opera resize adobe desktop flash ie10 internet explorer 10 metro windows 8 bcsp Advanced System Reporter reporting framework ControlMode form control master page placeholder publishing console SharePoint 2007 SharePoint error search search results search values software testing testing scenario audit content information architecture retention schedules PowerShell QuickLaunch scripts SharePoint server 2010 business solutions metalogix replication replicator storagepoint stena technet UK Technet picture library slideshow web part RTM released to manufacturing caml caml query MOSS 2007 query infopath