Quick Guide to Docker

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

ConceptWhat it is
ImageA read-only template defining the filesystem and runtime. Built from a Dockerfile.
ContainerA running instance of an image. Isolated process with its own filesystem.
VolumePersistent storage that survives container restarts and removal.
RegistryA 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
FlagMeaning
-itInteractive + TTY (for shell sessions)
-dDetached / background mode
-p host:containerMap a host port to a container port
--nameAssign a name to the container
--rmAuto-remove container on exit
-v host:containerBind-mount a host path into the container
-e KEY=VALUESet an environment variable
--cpus NLimit 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"]
InstructionPurpose
FROMBase image to build on
WORKDIRSet working directory inside the image
COPYCopy files from host into the image
RUNExecute a command during the build
EXPOSEDocument which port the app listens on
CMDDefault 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.