Docker Cheat Sheet

Essential Docker commands for images, containers, volumes, networks, Docker Compose, and Dockerfile instructions.

Images

CommandDescription
docker imagesList local images
docker pull image:tagDownload image from registry
docker build -t name:tag .Build image from Dockerfile in current dir
docker build -f path/Dockerfile .Build from specific Dockerfile
docker build --no-cache -t name .Build without cache
docker tag img:tag newname:tagTag an image
docker push image:tagPush image to registry
docker rmi imageRemove an image
docker rmi $(docker images -q)Remove all images
docker save -o file.tar imageSave image to tar file
docker load -i file.tarLoad image from tar file
docker history imageShow image layer history

Containers

CommandDescription
docker psList running containers
docker ps -aList all containers (including stopped)
docker run imageCreate and start container
docker start containerStart a stopped container
docker stop containerGracefully stop container
docker restart containerRestart container
docker kill containerForce stop container
docker rm containerRemove stopped container
docker rm -f containerForce remove (even if running)
docker rename old newRename a container
docker exec -it container bashOpen interactive shell in running container
docker exec container cmdRun command in container
docker logs containerView container logs
docker logs -f containerFollow logs in real-time
docker logs --tail 100 containerLast 100 log lines
docker cp container:/path ./localCopy files from container
docker cp ./local container:/pathCopy files to container

Run Options

FlagDescription
-dRun in detached mode (background)
-itInteractive + TTY (for shell access)
--name myappAssign a name to the container
-p 8080:80Map host port 8080 to container port 80
-PMap all exposed ports to random host ports
-v /host:/containerBind mount a volume
-v mydata:/dataUse a named volume
-e KEY=VALUESet environment variable
--env-file .envLoad env vars from file
--rmAuto-remove container when it stops
--restart alwaysAlways restart (unless manually stopped)
--restart unless-stoppedRestart unless explicitly stopped
--network mynetConnect to a specific network
-w /appSet working directory
--memory 512mLimit memory to 512MB
--cpus 2Limit to 2 CPUs
# Common run examples
docker run -d --name web -p 8080:80 nginx
docker run -it --rm node:20 bash
docker run -d --name db -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres:16

Volumes

CommandDescription
docker volume lsList volumes
docker volume create mydataCreate a named volume
docker volume inspect mydataShow volume details
docker volume rm mydataRemove a volume
docker volume pruneRemove all unused volumes
Tip: Named volumes (-v mydata:/data) persist across container restarts. Bind mounts (-v /host/path:/data) map to host filesystem and are great for development.

Networks

CommandDescription
docker network lsList networks
docker network create mynetCreate a bridge network
docker network inspect mynetShow network details
docker network connect mynet containerConnect container to network
docker network disconnect mynet containerDisconnect from network
docker network rm mynetRemove a network
docker network pruneRemove unused networks

Dockerfile

InstructionDescription
FROM image:tagBase image
WORKDIR /appSet working directory
COPY src destCopy files from host to image
ADD src destCopy + auto-extract archives + URL support
RUN commandExecute command during build
CMD ["cmd", "arg"]Default command when container starts
ENTRYPOINT ["cmd"]Command that always runs (CMD becomes args)
ENV KEY=VALUESet environment variable
ARG NAME=defaultBuild-time variable
EXPOSE 8080Document which port the container listens on
VOLUME /dataCreate a mount point
USER usernameSet user for subsequent instructions
LABEL key="value"Add metadata
HEALTHCHECK CMD curl -f http://localhost/Container health check

Example: Node.js App

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Example: Python App

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]
Tip: Copy dependency files first, then RUN install, then copy source code. This maximizes Docker layer caching — dependencies only reinstall when the lock file changes.

Docker Compose

CommandDescription
docker compose upStart all services
docker compose up -dStart in detached mode
docker compose up --buildBuild images before starting
docker compose downStop and remove containers
docker compose down -vStop and remove containers + volumes
docker compose psList running services
docker compose logs -fFollow logs for all services
docker compose logs serviceLogs for specific service
docker compose exec service bashShell into running service
docker compose restart serviceRestart a specific service
docker compose pullPull latest images
docker compose configValidate and view merged config

Example: compose.yaml

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/mydb
    depends_on:
      - db
    volumes:
      - ./src:/app/src

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  pgdata:

Cleanup

CommandDescription
docker system pruneRemove stopped containers, unused networks, dangling images
docker system prune -aAlso remove all unused images
docker system prune -a --volumesRemove everything unused (including volumes)
docker system dfShow Docker disk usage
docker container pruneRemove stopped containers
docker image pruneRemove dangling images
docker volume pruneRemove unused volumes

Inspect & Debug

CommandDescription
docker inspect containerDetailed container info (JSON)
docker statsLive resource usage (CPU, memory, I/O)
docker top containerRunning processes in container
docker port containerShow port mappings
docker diff containerShow filesystem changes
docker eventsReal-time Docker events
docker versionDocker version info
docker infoSystem-wide Docker info