Sitecore Docker cheat sheet

Abstract

An overview of the most important commands for managing Sitecore with Docker.

The official Docker documentation is very good, but it can be difficult to navigate. This topic lists some of the most useful commands for managing Sitecore with Docker in local development environments.

The examples assume that you use PowerShell as your command shell.

These commands show various information about Docker itself:

  • To list all available commands:

    docker help
    docker-compose help

    You can display detailed information for any specific command using --help:

    docker <command> --help
    docker-compose <command> --help
  • To display high-level information for your Docker environment (version, root directory, default isolation mode, and so on):

    docker info

You use these commands to manage images:

  • To list images (use -a to include intermediate images):

    docker image ls
    docker images

    You can format the results with the --format option. See the Docker documentation for a list of valid placeholders.

  • To remove images:

    docker image rm <image>
    docker rmi <image>

    The <image> can be either the image ID or full name. The ID only requires the minimum number of characters to uniquely identify it. For example, given this image list:

    REPOSITORY                            TAG       IMAGE ID      CREATED       SIZE
    mcr.microsoft.com/windows/servercore  ltsc2019  8351e66084ac  2 months ago  4.82GB
    mcr.microsoft.com/windows/nanoserver  1809      880394ef5494  2 months ago  251MB

    All 3 of these commands remove the nanoserver image:

    docker image rm mcr.microsoft.com/windows/nanoserver
    docker rmi 880394ef5494
    docker rmi 88
  • To remove all images:

    docker rmi $(docker images -a -q)

    To be more selective, use image list formatting combined with findstr. For example, to remove all images with a specific name or tag:

    docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}"|findstr "<search_text>")
  • To inspect images:

    docker image inspect <image>
    docker inspect <image>

    This displays detailed information for an image, including:

    • ID: The full unique identifier of the image.

    • WorkingDir: The folder you are in when you run an interactive shell in the image. You use this when you extend an image with your own customizations.

    • Entrypoint: The default entrypoint when you run this image in a container. This is useful information when you use the image in Docker Compose, which allows overriding.

    • VirtualSize: The size of the image in bytes.

    You can format the output.

You use these commands to manage containers:

  • To list containers (use -a to include stopped containers):

    docker container ls
    docker ps

    You can filter the results using the -f (or --filter) option. For example, to show just the running Sitecore CM images:

    docker ps -f "name=cm"

    You can format the results with the --format option. See the Docker documentation for a list of valid placeholders.

  • To remove containers (use -v to remove volumes as well):

    docker container rm <container>
    docker rm <container>

    Similar to images, the <container> can be either the container ID (full or partial) or full name.

  • To remove all stopped containers:

    docker rm $(docker ps -a -q)

    (Add --force to remove running containers as well.)

  • To inspect containers:

    docker container inspect <container>
    docker inspect <container>

    This displays detailed information for a container, including the following:

    • ID - the full unique identifier of the container.

    • Image - the image the container is running.

    • NetworkSettings - the network information including the Ports, IPAddress, and any Aliases.

    • LogPath - the file system path to the container's log file.

    • Volumes - displays any volume mappings between the host system and the container.

    • WorkingDir - this is where you are dropped when running an interactive shell in the container.

    You can format the output.

  • To start and stop containers individually:

    docker container start <container>
    docker start <container>
    docker container stop <container>
    docker stop <container>

    However, when you develop with Sitecore, you usually have multiple containers and use Docker Compose to start and stop containers.

  • To copy files between containers and the local file system:

    docker cp <src_path> <container>:<dest_path>
    docker cp <container>:<src_path> <dest_path>

    For example, to copy a file to a container:

    docker cp file.txt 2c26f76568d4:/tools/

    Or to copy a folder from a container:

    docker cp 2c26f76568d4:/inetpub/wwwroot/App_Config/ ./

    Note

    Your containers must be running with process isolation for this. File system operations with Hyper-V container are not supported.

  • To display logs:

    docker container logs <container>
    docker logs <container>

    You can stream the log output using -f (or --follow):

    docker logs -f <container>

    Type Ctrl+C to exit.

    Most containers generate a lot of entries, so you can use the --tail or --until options to limit the amount. For example, to display only the last 20 log entries:

    docker logs -f --tail 20 <container>

Many of the Docker commands provide the --format or -f option to format results, which allows you to format the output for display or even to pass to other scripts. The format string follows Go templating.

For inspect commands, the output data is already in JSON format, so the data structure is straightforward.

Some examples:

  • To get a container's image name:

    docker inspect --format='{{.Config.Image}}' <container>
  • To get a container's IP address:

    docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>

    The output from list commands is in a table format. The Docker documentation (see here for images, here for containers) has details about the available properties, or you can format the output as JSON as follows:

    docker images --format "{{json .}}"
    docker ps --format "{{json .}}"
  • To show a custom image list:

    docker images --format "{{.ID}}: {{.Repository}}"
  • To show a custom container list, using the table directive:

    docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}"

You can open an interactive shell prompt inside a Windows image. You can only use powershell in images that support it. If omitted, the default cmd shell is used:

docker run -it --rm <image> [powershell]

Type exit to return out of the container to your previous shell session.

This starts a new container (run), goes into an interactive shell (-it), and then destroys the container after you exit (--rm).

This does not work for all images, depending on the default ENTRYPOINT. If this is the case, run it in detached mode:

docker run -d <image>

Then run an interactive shell in the running container.

You can open an interactive shell prompt inside a running Windows container using the following.You can only use powershell in containers that support it:

docker exec -it <container> powershell
docker exec -it <container> cmd

Type exit to return out of the container to your previous shell session.

Docker does not remove unused resources automatically. Unnecessary resources can therefore accumulate over time and use up disk space.

The most useful command to solve this issue is:

docker system prune

By default, you are prompted to continue. To bypass, use the -f (or --force) option.

This command removes:

  • All stopped containers.

  • All networks not used by at least one container.

  • All dangling images (not tagged and not referenced by any container).

  • All build caches.

This default is generally safe to use at any time. You can add the following options to be more aggressive about it:

  • --volumes - Removes all volumes not used by at least one container

  • -a (or --all) - Removes all images without at least one container associated with them

There are prune commands for each individual Docker object (image, container, and so on), but those are used less often. See the Docker documentation for details.

You run these commands from the location of your Compose file. They assume that your Compose file is called docker-compose.yml. They will also automatically load an additional docker-compose.override.yml, if such a file is present.

If your Compose files are named something else, use the -f flag to specify explicitly:

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

You can specify as many files as you need. Compose combines them into a single configuration based on the order, with subsequent files overriding/adding to their predecessors.

You can view the aggregated result using config. For example:

 docker-compose -f docker-compose.yml -f docker-compose.dev.yml config

This shows your resolved application configuration, including combined compose files, .env, and so on. You can also add the name of one or more services. This allows you to target individual services or containers. For example, given the following Compose file:

version: "3"
services:
  mssql: [...]
  solr: [...]
  id: [...]
  cm: [...]

You can restart just the id and cm containers:

docker-compose restart id cm
  • To create and start containers:

    docker-compose up -d

    This creates and run containers for all services that you have defined in your Compose configuration.

    The -d (detached mode) starts the containers in the background and leaves them running. If you omit this, the container logs are streamed to the output, and you need to type Ctrl+C to return to a prompt. This also stops and removes your containers.

  • To stop containers:

    docker-compose stop

    This stops containers but does not remove the containers.

  • To start containers:

    docker-compose start

    This starts existing containers that you have previously stopped.

  • To restart containers:

    docker-compose restart

    This restarts all stopped and running containers.

  • To stop and remove containers:

    docker-compose down

    This stops all containers gracefully, and once all are stopped, it removes them. Any networks created by up are also removed. Use -v to remove volumes.

  • To list containers:

    docker-compose ps

    You can list the names of the services instead with the --services option. Use -a to include stopped containers.

  • To build images:

    docker-compose build

    This builds and creates images for all services that define a build.

    You can use the --build option with up. This rebuilds and runs containers with the latest code:

    docker-compose up --build -d
  • To display logs:

    docker-compose logs

    This displays the logs from all containers. You can use the --tail option to limit the number of lines, and filter for specific containers. For example, to display only the last 20 log entries from your cm and xconnect containers:

    docker-compose logs --tail 20 cm xconnect

    You can also stream the log output with the -f (or --follow) option:

    docker-compose logs -f --tail 20

    Type Ctrl+C to exit.