Docker Quick Reference

A compact lookup for Dockerfile instructions, Compose keys, and the docker and docker compose commands.

Before you use this reference

This page is a lookup, not a tutorial. The explanations are in Why Containers, Writing a Dockerfile, Docker Compose Basics, and Building the Data Stack.

Core vocabulary#

TermMeaning
ImageImmutable package: file system + default command
ContainerA running instance of an image
RegistryWhere images are stored and pulled (Docker Hub)
VolumeStorage that outlives a container
Bind mountA host path mounted into a container
Compose projectA set of services managed together from one file

docker commands#

Bash
docker run --rm -p 8123:8123 clickhouse/clickhouse-server:latestdocker ps                       # running containersdocker ps -a                    # including stopped onesdocker logs -f <name>           # follow a container's outputdocker exec -it <name> bash     # shell inside a running containerdocker stop <name>              # stop itdocker rm <name>                # remove a stopped containerdocker images                   # local imagesdocker rmi <image>              # remove an imagedocker build -t name:tag .      # build an image from the current directorydocker system prune             # remove stopped containers, unused networks/images

docker run flags:

FlagEffect
--rmDelete the container when it stops
-p H:CPublish container port C on host port H
-e KEY=valueSet an environment variable
-v name:/pathMount a named volume
-v ./path:/pathBind-mount a host directory
-dRun detached (in the background)
--nameGive the container a fixed name

docker compose commands#

Bash
docker compose up -d              # start all services in the backgrounddocker compose up -d --build      # rebuild images firstdocker compose ps                 # status of each servicedocker compose logs -f <service>  # follow one service's logsdocker compose exec <service> sh  # shell inside a servicedocker compose build              # build images for services with `build:`docker compose stop               # stop containers, keep themdocker compose start              # start stopped containersdocker compose restart <service>  # restart one servicedocker compose down               # stop and remove containers + networkdocker compose down -v            # also delete named volumes (wipes data)docker compose config             # print the resolved, merged config

Dockerfile instructions#

DOCKERFILE
FROM image:tag            # base image; always pin the tagUSER name                 # user for later instructions and the containerRUN command               # run at build time; each RUN is a cached layerCOPY src dst              # copy from the build context into the imageENV KEY=value             # environment variable baked into the imageWORKDIR /path             # working directory for later instructionsEXPOSE 8080               # documents a port (does not publish it)CMD ["executable", "arg"] # default command when the container startsENTRYPOINT ["executable"] # fixed command; CMD becomes its arguments

Layer-cache rule: order instructions least-changed first. Install system and Python packages before COPY-ing source.

Compose file keys#

YAML
services:  name:    image: repo/name:tag         # OR    build: .                     # build from ./Dockerfile    container_name: name         # fixed name instead of the generated one    command: ["..."]             # override the image's default command    ports:      - "HOST:CONTAINER"    environment:      - KEY=value                # list form      KEY: value                 # OR map form    volumes:      - named_volume:/in/container      - ./host/path:/in/container    depends_on:      other:        condition: service_healthy    healthcheck:      test: ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"]      interval: 10s      timeout: 5s      retries: 5      start_period: 30s    restart: always              # always | unless-stopped | "no"
volumes:  named_volume:                  # declare every named volume here

Networking rule#

Talking fromUse address
Your machinelocalhost:<published-host-port>
One container to another<service-name>:<container-port>

Inside a container, localhost is that container, not the host or another service.

Volumes#

KindSourceDeleted byUse for
Named volumea name under top-level volumes:docker compose down -vdatabase data directories
Bind mounta host path (./dags)never (it is your file)source code, config, logs

Common failure to check first#

SymptomUsual cause
port is already allocatedAnother process (or a stale container) uses that host port
Service connects then hangsWrong host: used localhost instead of the service name from a container
Container exits immediatelyIts command finished or failed; check docker compose logs <service>
Data gone after restartPath was not on a volume, or down -v was run
Healthcheck never passesWrong test command, or start_period too short

See also#