Tips for managing Sitecore containers

In this post, I will show you some useful commands and tips for managing your Sitecore containers, images and other docker resources that get created when standing up your containers. The Docker documentation is awesome but it is fairly extensive so I’ll cover some useful commands you should be familiar with.

PS> docker container ls – lists running containers by default, you need to use -a flag to list all containers not just running containers. You can use -f flag to filter containers.

PS> docker container ls -f “name=93x*”

dockercontainerslsfilter

If you want to see the most recently created use -l flag.

PS> docker image ls  –  returns a list of images. Again you can easily filter this list by name and tag.

PS> docker volume ls – list all volumes. Use the –filter or -f (shorthand) flag to specify a filter.

PS> docker network ls – list all know networks by the docker daemon.

Wait there is more…

By default, docker does not necessarily provide you with all the available output in the list command or other commands for that matter. Docker uses the Golang templating engine to translate a json result object to a more aesthetic output. Where a command has the –format flag you can specify your own format. To see all the available fields for the Container list command:

PS> docker container ls –format ‘{{ json . }}’

dockerformatjson

So you can format to output to display only the fields you need by specifying your own format for example if we only wanted the id, Status, and Image we could use the following format where ‘\t’ denotes a tab.

PS> docker container ls –format ‘{{.ID}}\t{{.Status}}\t{{.Image}}’

dockerformatexample

This can also be cleaned up and displayed in a table:

PS> docker container ls –format ‘table {{.ID}}\t{{.Status}}\t{{.Image}}’

dockerformattable

The –format flag and golang templating is pretty useful as it allows you to construct/control the output from docker commands so it can be passed to other scripts.

Inspecting Containers and Images

The docker inspect command provides additional information about containers and images and other resources. It accepts the name or id of the resource and can inspect multiple resources of the same type.

PS> docker inspect container [options] <container id/name>

Most of the output is self-explanatory, here is some the information provided you might find useful:

  • NetworkSettings – the network environment for the container. This includes the IP address which you will need to remote debug your running Sitecore instance in Visual Studio.
  • Image – the image the container is running.
  • LogPath – the path to this container’s log file. The log file can provide some useful information when troubleshooting issues with a container. For example, if you are having issues mounting volumes or updating the files in your Sitecore container you might want to check here for errors.ContainerLogPath
  • RestartCount – the number of times the container has been restarted.
  • Volumes – defines the volume mapping between the host system and the container.
  • HostConfig Key configurations for how the container will interact with the host system.
  • Config – The runtime configuration options set when the docker run command was executed.

If we format the output if can retrieve just the IP address:

PS> docker container inspect –format “{{ range .NetworkSettings.IPAddress }}{{.IPAddress}}” <container id/name>

Or the LogPath:

PS> docker container inspect -format “{{ .LogPath }}” <container id/name>

The command for inspecting images is similar

PS> docker inspect image [Options] <image id or name>

  • ID – the unique identifier of the image.
  • Parent – the identifier your image is based on.
  • Container – Docker will create a container during the image construction process, and this identifier is stored in the image data. This container identifier of the temporary container created when the image was built.
  • ContainerConfig – This data again is referring to the temporary container created when the Docker build command was executed.
  • DockerVersion – version of Docker used to create the image. Useful as the Docker continues to evolve and new features are Mae available.
  • Virtual Size – the size of the image in bytes.

You can also inspect Networks and Volumes.

PS> docker network inspect <network id/name> – returns information about or more networks.

PS> docker volume inspect <volume id/name> – returns information about or more volumes.

Removing Docker Resources

Docker does not automatically remove unused resources. Over time you can easily accumulate unnecessary resources eating up disk space.

PS> docker system prune – is extremely useful for cleaning up unused resources quickly as it will remove all stopped containers, networks not used by containers, dangling images or –all images without a container and all build caches. By default, volumes are not deleted this prevents you from accidentally removing important data you may want to keep. To remove volumes you need to specify the –volumes flag.

systemprune

If you prefer more granular control of the resources you want to remove docker provides the following:

PS> docker container prune – will remove stopped containers. You can also specify –filter flag to filter specific containers to prune. For example, if I run the following command listing all containers: docker container ls –all  :

allcontainers

As you can see from this list there are containers that have excited. An exited container is a normal state for a container when the main process running in it has exited. Normally a graceful exit is expected and it should have a status code (0) anything else indicates something went wrong the status code should help you figure out what went wrong. Have you ever tried to run docker-compose up -d and received an error about a resource not available like network or something? This is most likely a symptom of a container not exciting gracefully perhaps you restarted windows before stopping your containers or something like. When this happens don’t fret usually a docker-compose down followed by docker-compose up resolves the issue. Or simply removing the exited containers resolves the issue.

docker container ls –all –filter “status=exited” gives me a list of just the containers that have exited.

containersexcited

If I run docker container prune it will remove these exited containers

prunecontainers

PS> docker image prune – will remove images. It has two modes of operation 1. remove dangling images (default) i.e images that are not tagged. 2. Unused images not just dangling one by specifying the -a flag.

PS> docker volume prune – remove unused volumes.

PS> docker network prune – removes all networks not used by at least one container.

You can also remove individual resources using the docker rm command or you could remove a group of resources using the –filter flag.

Starting and stopping Containers

PS> docker-compose up – starts and runs your entire app. You need to run docker-compose up from the location of your docker-compose.yml for or use the -f flag to specify your compose file.

PS> docker-compose stop – stops your containers but it will not remove them.

PS> docker-compose start – starts existing containers. Remember to use docker container ls -a to get a list of all containers not just running containers.

PS> docker-compose down – stops your containers and removes them and any networks. Use the -v to remove all volumes.

PS> docker-compose kill – forces containers in your composition to stop abruptly.

You can also run similar commands without compose to start and stop individual containers.

Additional Info

I hope you find this helpful as you explore Sitecore on Docker!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s