5 Essential Docker Commands for Sitecore Developers

Streamline Sitecore development with these essential Docker commands.

May 24, 2023

By Karan Patel

Docker has become an essential technology for modern software development and deployment, allowing developers to easily create and manage containers. Docker provides an effective way for Sitecore developers to build, test, and deploy Sitecore solutions. This blog article will go through five fundamental Docker commands that any Sitecore developer should be familiar with. These commands will help you optimize your Docker environment and streamline your development routine. Before we start, if you don’t know what Docker is, then check out our blog for a brief Introduction to Docker. Alright, let's get started!

docker ps -a

The docker ps -a command is used to list all the containers on your system, whether they are running or stopped. This command is particularly helpful when you want to view the status of your Docker containers and check if they are running as expected. It provides a summary of essential information such as the container ID, image used, status, and ports mapping.

Below is a screenshot of when I was working on an upgrade project and many times received the error “for traefik container is unhealthy” even though the containers always have the done next to them but the output never suggested anything peculiar.

Docker Commands

In order to find out which container is unhealthy, we can put docker ps -a command to use and now I know that my cd container is messed up. Well… it's a start. If you’re using the Docker extension for VS Code, which we highly recommend, then you can quickly check the logs for the container and find out why it is unhealthy.

Docker Commands

docker inspect

The docker inspect command provides detailed information about a container or image, including its configuration, network settings, volumes, and more. It is an invaluable command for troubleshooting issues and understanding the internals of a container.

Working with Docker presents its own challenges, one of which arose when I tried connecting to a SQL Server instance to run upgrade scripts during a Sitecore version upgrade. Fortunately, the docker inspect command can make this task easier. To use it, first execute the docker ps -a command to obtain the container ID and then plug it into docker inspect as shown below.

docker inspect <container id>

You will receive a JSON-formatted output containing the IPAddress of the container from which you can connect to your SQL Server.

Docker Commands

docker cp

The docker cp command allows you to copy files and directories between your local machine and a running Docker container. This command is useful when you need to transfer files to or from a container, such as configuration files, scripts, or data.

During our upgrade project, we had to install "Coveo for Sitecore". Technically, you can use the image built for it, but Coveo doesn’t provide any support for the images. More information can be found here. Therefore, it's better to do a package install instead. To ensure safety, we had to back up the necessary config files installed by the Coveo for Sitecore package. For this, we can use the docker cp command. As before, first execute the docker ps -a command to get the container ID, as we'll need it for the docker cp command. Once you have the container ID, stop the container and then use the docker cp command.

docker stop <container id>
docker cp  'container id:/path/in/container/file.txt' '/path/to/local/file.txt'

Let’s see the command in action.

Docker Commands Docker Commands

docker-compose build --no-cache

Sitecore solutions often involve multiple containers orchestrated using docker-compose. The docker-compose build command builds or rebuilds the Docker images defined in your docker-compose.yml file. Adding the --no-cache option ensures that Docker does not use the cache when building the images. When you’re working on multiple projects that use Docker, sometimes you’ll have image caches interfering with each other when switching between projects.

Below, you can see two different project containers referencing the same SQL server image. To avoid this, use the docker-compose build --no-cache command when building your images.

Docker Commands

After executing the command,

Docker Commands Docker Commands

docker system prune

Over time, your Docker environment accumulates unused resources such as dangling images, stopped containers, and unused networks. The docker system prune command helps you clean up these unused resources, freeing up disk space and improving the performance of your Docker environment.

Docker Commands

Look at all the unused resources above, to clean up unused resources, simply execute the command docker system prune. However, be extremely careful when using this command as it may remove necessary resources. This is why I like to call this command "The Decimation," after Thanos' snap.

Docker Commands

Summary

That’s it, folks. Mastering these essential Docker commands can greatly enhance your efficiency and productivity as a Sitecore developer. The ability to effectively manage containers, troubleshoot issues, and optimize the Docker environment is crucial for seamless Sitecore development and deployment. Incorporate these commands into your workflow to unlock the full potential of Docker for Sitecore development. Here is a cheat sheet for Docker commands provided by Sitecore. However, for a full list of Docker commands look at Docker’s documentation.

Happy decoding!



Karan Developer

Karan Patel

Sitecore Developer

Karan is a Sitecore Certified Developer with over 2 years of experience building and implementing Sitecore solutions using ASP .NET MVC, React, Azure and AWS. He's an avid fan of Counter-Strike and can usually be found playing it if he is not developing. In his spare time, he likes to play guitar and cook Indian food.