Sitecore Docker cheat sheet

Version: 10.1

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.

General commands

These commands show various information about Docker itself:

  • To list all available commands:

    RequestResponse
    docker help
    docker compose help

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

    RequestResponse
    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):

    RequestResponse
    docker info

Manage images

You use these commands to manage images:

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

    RequestResponse
    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:

    RequestResponse
    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:

    RequestResponse
    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:

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

    RequestResponse
    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:

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

    RequestResponse
    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.

Manage containers

You use these commands to manage containers:

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

    RequestResponse
    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:

    RequestResponse
    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):

    RequestResponse
    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:

    RequestResponse
    docker rm $(docker ps -a -q)

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

  • To inspect containers:

    RequestResponse
    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:

    RequestResponse
    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:

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

    For example, to copy a file to a container:

    RequestResponse
    docker cp file.txt 2c26f76568d4:/tools/

    Or to copy a folder from a container:

    RequestResponse
    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:

    RequestResponse
    docker container logs <container>
    docker logs <container>

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

    RequestResponse
    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:

    RequestResponse
    docker logs -f --tail 20 <container>

Format results

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:

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

    RequestResponse
    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:

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

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

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

Open an interactive shell in an image

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:

RequestResponse
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:

RequestResponse
docker run -d <image>

Then run an interactive shell in the running container.

Open an interactive shell in a 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:

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

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

Cleanup resources

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:

RequestResponse
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.

Use Docker Compose

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:

RequestResponse
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:

RequestResponse
 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:

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

You can restart just the id and cm containers:

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

    RequestResponse
    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:

    RequestResponse
    docker compose stop

    This stops containers but does not remove the containers.

  • To start containers:

    RequestResponse
    docker compose start

    This starts existing containers that you have previously stopped.

  • To restart containers:

    RequestResponse
    docker compose restart

    This restarts all stopped and running containers.

  • To stop and remove containers:

    RequestResponse
    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:

    RequestResponse
    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:

    RequestResponse
    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:

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

    RequestResponse
    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:

    RequestResponse
    docker compose logs --tail 20 cm xconnect

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

    RequestResponse
    docker compose logs -f --tail 20

    Type Ctrl+C to exit.

Do you have some feedback for us?

If you have suggestions for improving this article,