Cheatsheets:Docker
Quick reference for smart people — part of our dev cheatsheets collection.
Docker packages software into lightweight, isolated containers, each run from a read-only image.
New to Docker? Get Started runs your first container; the CLI reference documents every command.
Basic example: serve a website
docker pull nginx # fetch the image
docker run -d --name web -p 8080:80 nginx # start a container from it
docker ps # is it running?
curl localhost:8080 # yes — the nginx welcome page
docker stop web && docker rm web # stop and remove it
An image is a read-only template; a container is one running instance of it. Containers are throwaway — anything written inside them vanishes when the container is removed, unless kept in a volume.
Running containers
docker run -it ubuntu bash # interactive terminal
docker run -d --name db postgres:16 # detached, named
docker run -p 8080:80 nginx # host 8080 → container 80
docker run -v ./data:/app/data app # mount a host directory
docker run --rm -it app # remove the container on exit
docker run -e MODE=prod app # set an environment variable
| Flag | Effect |
|---|---|
-d |
run detached (in the background) |
-it |
interactive terminal (needed for bash) |
-p host:container |
publish a port |
-v host:container |
mount a volume or directory |
--name |
give the container a name |
--rm |
auto-remove once it stops |
-e KEY=value |
set an environment variable |
Port order is always host:container —
-p 8080:80means your machine's 8080 forwards to the container's 80, not the other way round.
Managing containers
docker ps # running containers
docker ps -a # all, including stopped
docker logs -f web # follow the logs
docker exec -it web bash # shell inside a running container
docker cp web:/etc/nginx/nginx.conf . # copy a file out of a container
docker stop web # stop one
docker start web # start an existing (stopped) one
docker rm web # remove a stopped container
docker runalways creates a new container. Re-run the same task?docker stop/docker startthe existing one instead — or use Compose.
Images
docker build -t myapp . # build an image from the Dockerfile
docker images # list local images
docker pull postgres:16 # fetch an image (or a tag)
docker push user/myapp:v1 # upload to a registry
docker tag myapp user/myapp:v1 # retag
docker rmi myapp # remove an image
Dockerfile
A plain text recipe that docker build turns into an image:
FROM python:3.12-slim # base image
WORKDIR /app # working directory for the rest
COPY requirements.txt . # copy file(s) into the image
RUN pip install -r requirements.txt
COPY . . # everything else
CMD ["python", "app.py"] # default command when run
Volumes
docker run -v ./data:/app/data app # bind mount: a host directory
docker run -v data-vol:/data app # named volume, managed by Docker
docker volume ls # list named volumes
docker volume prune # remove unused ones
Without a volume, container data is lost on
docker rm. The database keeps its data only because something like-v db-data:/var/lib/postgresql/datais mounted.
Docker Compose
Multi-container setups described in YAML — start, stop and rebuild everything with one command:
services:
web:
build: . # build from the Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/app/data
db:
image: postgres:16 # or pull a ready-made image
environment:
POSTGRES_PASSWORD: secret
docker compose up -d # start everything in the background
docker compose up --build # rebuild images, then start
docker compose ps # status of all services
docker compose logs -f # follow the combined logs
docker compose down # stop and remove the containers
The modern command is
docker compose(space). The old v1 tool was a separate binary spelleddocker-compose(hyphen) — you may still see it in older tutorials.
Cleanup
docker system prune # dangling images, stopped containers, unused networks and cache
docker system prune -a # ... plus every image no container uses
docker system df # disk usage by images, containers, volumes
docker system prune -aremoves images you are not running. Images you might want again later need adocker pull— they are not lost, just re-fetched.
Further reading
- Docker documentation — the official docs
- Get Started — the recommended tutorial
- Docker CLI reference — every command and flag
- Compose documentation — multi-container applications