Joshua's Cheatsheets - Docker Cheatsheet, notes, and more


What Type Link
Base Commands - Quick Ref Reference docs.docker
Compose - getting started Tutorial docs.docker
Compose - getting started Tutorial fedoraproject
Compose Guide (Heroku) Guide Heroku
Example of a YAML config for a complex backend/frontend docker setup Tutorial freecodecamp
Devhint Cheatsheet Reference

Basic Commands

What Command Helpful Flags
List active containers docker ps --all
Restart a container docker restart {containerName}
Stop a container docker kill {containerName}
Inspect something docker inspect {thingIdOrName}
View logs docker logs {?:containerName} -f follow
--details show extra detail
Execute a command inside a container docker exec {containerName} -i interactive
-t allocate TTY
-d detach
Create volume docker volume create {options} {volumeName} --driver (see notes on volume types)
List volumes docker volume ls
Drop volume docker volume rm {volumeName} -f or --force
Print version docker version
List images docker image ls
Pull an image docker pull {imageName}
Run an image docker run {imageName}

Example: docker run hello-world
--name (give container a short name)
List downloaded images docker images OR docker image ls --filter

Note: For most of the commands that take an image name, you can optionally request a specific tag or digest version of an image by using a special suffix. For example, {imageName}:{tag} or {imageName}@sha256:{digestSha}

docker-compose is listed below, in its own section

Docker Compose (aka docker-compose) commands

CLI Overview:, or use docker-compose --help Composer YML file overview:

There are common flags that can be applied with any subcommand. For example: -f or --file lets you specify a docker .yml file other than the default of docker-compose.yml.

What Command Helpful Flags
Test out a config docker-compose config
Stop containers, without removing docker-compose stop
Stop and remove containers docker-compose down -v: remove named volumes
Build and launch container docker-compose up -d (starts in detached mode)
Start a specific service docker-compose run {serviceName}
Removed stopped containers docker-compose rm If you don't pass a name, it removes all (like rm *)
-v removes the volumes as well. Useful if something is persisting when you don't want it to and need a hard reset.
-f : Force
Validate a config without running it docker-compose config -q - Quiet mode, only validates true/false without verbose output
Show logs docker-compose logs -f follow
--tail={#} Tail lines
You can also pass the container name - docker-compose logs {containerName}

Docker-Compose syntax and options

  • You can use environment values within your docker-compose file (variable substitution)

    • Syntax is ${VARIABLE_NAME} (just like JS template literal), or $VARIABLE_NAME
    • The .env file has to be in the same directory as the docker-compose.yml file (see 1, 2)

      • Depending on your version of docker-compose, you might be able to get around this with either --project-directory (ref) or --env-file (ref), however, I have not had success with either so far
  • You can pass environment values to the container itself

    • Full details

      • You can use the env_file option to pass an entire .env file contents to the container
      • Put under environment: but leave off value
      • Pass via docker-compose run -e {VAR}={VAL} {imageName}

Volume types options / drivers


Type Description
tmpfs Temp data, stored in memory, not persisted between sessions
btrfs Advanced, persisted storage. Requires a bunch of pre-reqs, including Linux Kernel support.
nfs Shared, persisted storage

How do i...

How do I...

  • List active containers

    • docker ps
  • Restart a container with changes after a .yml / yaml config chance?

    • Bring up, and force image rebuild:

      • docker-compose up -d --force-recreate --build
    • If you run into issues (changes don't seem to take affect, passwords not working, etc)...

      • Sometimes the volume data will persist, even if you ran docker-compose down -v first.
      • Try finding the volume data directory and running rm -rf on it, then rebuild and start back up
  • Get a container IP address

    • docker inspect {containerName}
    • Just IP address:

      • docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
      • Use double quotes if you have issues.
      • Credit: S/O
  • Debug a container not starting correctly (die event), or view logs

    • Multiple options for log viewing:

      • See this S/O

        • Summary:

          • Start event logger with docker events& - this kind of does tail
          • Do "thing" - e.g. up, that triggers error
          • You should see error appear in event log
          • Copy error instance ID from event log and use with docker logs {eventId}
      • docker-compose -f {composeFile} logs

        • Use -f to follow
        • Use --tail={#} to tail # of lines
      • docker logs {?:containerName}
  • Access BASH inside container

    • docker exec -ti {containerName} bash

      • Use exit to stop
    • You can also try docker attach {containerName} to link console STDIN/STDOUT, but this seems less optimal than exec
  • Remove a stubborn network ("error while removing network ... has active endpoints")

    1. Find out what containers are using the network

      • docker inspect {networkName}
    2. Either bring down those containers, or break the connection

      • To break connection(s), use docker network disconnect {networkName} {containerName}
    3. Once you have broken linkage, you should be able to bring down

      • Either re-run down, or manually remove network with docker network rm {networkName}
  • Kill *all containers

    • docker ps -q | xargs docker kill


  • PostgreSQL issues with Windows - usually due to permissions issue

  • Can't remove volume (even with -f or --force): "Error response from daemon: remove {volume}: volume is in use"

    • Try this first:

      • docker-compose down -v (or docker-compose rm -f -v)
    • If the volume is being locked by a container, you can find what is using it:

      • docker ps --filter volume={volume}
    • Try docker system prune, and then remove volumes again
    • As a last ditch effort, you can try restarting the entire Docker service (either via GUI, or CLI), then try removing container

    • Lots of tips in the responses to this S/O question
Markdown Source Last Updated:
Thu Mar 26 2020 08:23:44 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Thu Dec 05 2019 01:06:19 GMT+0000 (Coordinated Universal Time)
© 2020 Joshua Tzucker, Built with Gatsby