The Sitecore’s Github “docker-images” repository contains information on how to build the Docker images with Sitecore already installed. The following lines aim to fill a gup by explaining how to use those Sitecore Docker images once they have been built. Part of this information was posted by me as an answer to a question on the Sitecore’s Stack Exchange site.
There is a lot of content on the Internet explaining generic Docker concepts, therefore this blog post will focus on how to work with Sitecore related containers.
Please note that the Docker image and container identifiers used in the following examples will need to be updated with the values from your own environment.
Run a Sitecore container based on an image:
docker run -d -m 8GB -p 80 -p 443 -p8983 --name <ContainerName> <ImageNameOrID>
Examples:
docker run -d -m 8GB -p 80 -p 443 -p8983 --name test 'MyRepo.azurecr.io/sitecore-xpsingle:9.0.2.180604-windowsservercore-ltsc2016'
Or
docker run -d -m 8GB -p 80 -p 443 -p8983 --name test 846c823a2572
Access to the Sitecore instance that is running in the container
You can access the Sitecore instance from your computer with your preferred browser by following the next steps:
1.- Get the container IP
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' <ContainerNameOrId>
Or
docker exec <ContainerNameOrId> ipconfig
Examples:
docker exec test ipconfig
docker exec 5bfbd811908f ipconfig
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' test
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' 5bfbd811908f
2.- Update the local “host” file with the following entries
<ContainerIP> habitat.dev.local
<ContainerIP> habitat_xconnect.dev.local
Examples:
172.19.118.233 habitat.dev.local
172.19.118.233 habitat_xconnect.dev.local
172.19.118.233 solr
3.- Browse to any of the following URLs
The first time may take some time as Sitecore has to warm up:
If no changes have been done in the build files, the domain names must be literal.
Re-start a container that is stopped (with STATUS = ‘Exited’)
When a docker container stops, it can be restarted with the following command. Please notice that the IP will change after the container has been started, therefore, the “hosts” file will need to be updated with the new IP before browsing to Sitecore.
docker start <ContainerName>
Example:
docker start test
The Docker command docker ps
shows a list of running containers. If you want to see all containers, including those that are stopped you can add the parameter -a
as per the following example:
docker ps -a
Troubleshooting the container with PowerShell
Connect to a running container with a PowerShell console
docker exec -it <ContainerNameOrId> powershell
Examples:
docker exec -it test powershell
docker exec -it 5bfbd811908f powershell
Run a container with a volume that allows you to copy the Sitecore and IIS logs, and other data out of the container
docker run -d -m 8GB -p 80 -p 443 -p8983 -v E:\Debug:C:\Debug --name <ContainerName> <ImageNameOrID>
Examples:
docker run -d -m 8GB -p 80 -p 443 -p8983 -v E:\Debug:C:\Debug --name test 'MyRepo.azurecr.io/sitecore-xpsingle:9.0.2.180604-windowsservercore-ltsc2016'
docker run -d -m 8GB -p 80 -p 443 -p8983 -v E:\Debug:C:\Debug --name test 846c823a2572
Once the container is running, you can connect with a PowerShell console as described above. Inside the container, anything that is copied to the folder C:\Debug will be automatically saved in the container’s host in “E:\Debug”.
Nesting Docker commands
The docker commands can be nested to chain several steps into one single command. Next, there are some useful examples:
Run a container and immediately open a PowerShell console attached to the container
docker exec -it (docker run -d -m 8GB -p 80 -p 443 -p8983 --name <ContainerName> <ImageNameOrID>) powershell
Examples:
docker exec -it (docker run -d -m 8GB -p 80 -p 443 -p8983 --name test 'MyRepo.azurecr.io/sitecore-xpsingle:9.0.2.180604-windowsservercore-ltsc2016') powershell
Or
docker exec -it (docker run -d -m 8GB -p 80 -p 443 -p8983 --name test 846c823a2572) powershell
Scripting all the steps together
The modification of the host file can be easily automated by using a PowerShell module named “Carbon” which is well documented here.
The following PowerShell Script runs a Sitecore container, opens a PowerShell console attached to the container once it is running and also modifies the “host” file with the container’s IP so that it can be browsed afterward:
$containerName = <ContainerName>; $imageId = <ImageNameOrID>; `
$containerId = (docker run -d -m 10GB -p 80 -p 443 -p8983 -v C:\Debug:C:\Debug --name $containerName $imageId); `
$ipAddress = ((docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $containerId).Trim()); `
'solr', 'habitat_xconnect.dev.local', 'habitat.dev.local', 'habitathome.dev.local'|ForEach-Object {Set-HostsEntry -IPAddress $ipAddress -HostName $_}; `
docker exec -it $containerName powershell
Examples:
$containerName = 'test'; $imageId = 'MyRepo.azurecr.io/sitecore-xpsingle:9.0.2.180604-windowsservercore-ltsc2016'; `
$containerId = (docker run -d -m 10GB -p 80 -p 443 -p8983 -v C:\Debug:C:\Debug --name $containerName $imageId); `
$ipAddress = ((docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $containerId).Trim()); `
'solr', 'habitat_xconnect.dev.local', 'habitat.dev.local', 'habitathome.dev.local'|ForEach-Object {Set-HostsEntry -IPAddress $ipAddress -HostName $_}; `
docker exec -it $containerName powershell
Or
$containerName = 'test'; $imageId = 846c823a2572; `
$containerId = (docker run -d -m 10GB -p 80 -p 443 -p8983 -v C:\Debug:C:\Debug --name $containerName $imageId); `
$ipAddress = ((docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' $containerId).Trim()); `
'solr', 'habitat_xconnect.dev.local', 'habitat.dev.local', 'habitathome.dev.local'|ForEach-Object {Set-HostsEntry -IPAddress $ipAddress -HostName $_}; `
docker exec -it $containerName powershell
Please let me know below in case you have any question or comment, and consider sharing it if you find it useful.
Thanks for sharing! It’s useful in my major, because there is a sub major that is talking about coding. Thank you!
Thanks, Rinda, for sharing your thoughts.
Kind Regards,