Quick Reference Guide
Quick Guide to Docker
A practical reference for Docker. Covers images, containers, volumes, Dockerfile basics, Docker Compose, and cleanup. Docker Desktop is available for macOS, Windows, and Linux.
Note: This guide was originally published in 2018 and has been updated in May 2026 with additional sections and improved formatting, with assistance from Claude (Anthropic).
Core Concepts
| Concept | What it is |
| Image | A read-only template defining the filesystem and runtime. Built from a Dockerfile. |
| Container | A running instance of an image. Isolated process with its own filesystem. |
| Volume | Persistent storage that survives container restarts and removal. |
| Registry | A store of images. Docker Hub is the default public registry. |
docker --version
docker run hello-world
docker --help
docker <command> --help
Working with Images
docker image ls # list local images
docker pull ubuntu # pull latest Ubuntu image
docker pull ubuntu:22.04 # pull a specific tag
docker image rm ubuntu # remove a local image
docker image prune # remove unused dangling images
docker build -t myapp:1.0 . # build image from Dockerfile in current dir
docker tag myapp:1.0 myapp:latest # tag an image
docker push myrepo/myapp:1.0 # push to a registry
Running Containers
# Interactive terminal
docker run -it ubuntu bash
# Detached web server with port mapping and name
docker run -d -p 80:80 --name webserver nginx
# Auto-remove on exit, port mapped, CPU-limited
docker run -d -p 3000:3000 --cpus 2 --name react --rm alpine-npm /bin/ash
# Bind-mount current directory into container
docker run -it -v $(pwd):/workspace ubuntu bash
| Flag | Meaning |
-it | Interactive + TTY (for shell sessions) |
-d | Detached / background mode |
-p host:container | Map a host port to a container port |
--name | Assign a name to the container |
--rm | Auto-remove container on exit |
-v host:container | Bind-mount a host path into the container |
-e KEY=VALUE | Set an environment variable |
--cpus N | Limit CPU allocation |
Managing Containers
docker container ls # list running containers
docker container ls --all # list all (including stopped)
docker container stop webserver # stop gracefully
docker container start webserver # start a stopped container
docker container restart webserver
docker container rm webserver # remove a stopped container
docker container rm -f webserver # force-remove a running container
docker attach webserver # attach to container stdout
docker exec -it webserver bash # open a new shell in running container
# Ctrl-P then Ctrl-Q to detach without stopping
docker logs webserver # show container logs
docker logs -f webserver # follow logs in real time
docker inspect webserver # full JSON metadata
Volumes
Volumes persist data beyond the container lifecycle. Prefer named volumes over bind mounts for database data.
docker volume create mydata
docker volume ls
docker volume inspect mydata
docker volume rm mydata
# Mount a named volume
docker run -d -v mydata:/var/lib/mysql mysql
# Bind mount current directory
docker run -it -v $(pwd):/app -w /app python:3.12 bash
Dockerfile Basics
A Dockerfile defines how to build an image. A minimal Python app example:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
| Instruction | Purpose |
FROM | Base image to build on |
WORKDIR | Set working directory inside the image |
COPY | Copy files from host into the image |
RUN | Execute a command during the build |
EXPOSE | Document which port the app listens on |
CMD | Default command when the container starts |
Docker Compose
Compose runs multi-container applications defined in a docker-compose.yml:
services:
web:
build: .
ports: ["5000:5000"]
depends_on: [db]
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes: [pgdata:/var/lib/postgresql/data]
volumes:
pgdata:
docker compose up -d # start all services in background
docker compose down # stop and remove containers
docker compose logs -f # follow logs for all services
docker compose ps # list service status
docker compose build # rebuild images
Cleanup
docker container prune # remove all stopped containers
docker image prune # remove dangling images
docker image prune -a # remove ALL unused images
docker volume prune # remove unused volumes
docker system prune # containers + networks + dangling images
docker system prune -a # full clean (use with caution)
docker system df # show disk usage by Docker objects
Run docker system df first to see how much space Docker is consuming before pruning.
For running GUI applications from Linux containers on a Mac or Windows host, see this guide. Full documentation is at docs.docker.com.