Docker Cheatsheet

Table of Contents

  1. Images
  2. Containers
  3. Container Inspection
  4. Exec & Attach
  5. Volumes
  6. Networks
  7. Docker Compose
  8. Dockerfile Reference
  9. Build
  10. Registry & Push
  11. Cleanup
  12. Resource Limits
  13. Logs & Monitoring
  14. Environment Variables
  15. Useful Flags & Patterns

Images

Command Description
docker images List all local images
docker pull image:tag Pull image from registry
docker pull nginx:latest Pull latest nginx image
docker image inspect image Show image details
docker image history image Show image layer history
docker tag source:tag target:tag Tag an image
docker rmi image Remove image
docker rmi $(docker images -q) Remove all images
docker image prune Remove dangling images
docker image prune -a Remove all unused images
docker save -o file.tar image Export image to tar
docker load -i file.tar Import image from tar

Containers

Command Description
docker run image Create and start container
docker run -d image Run in detached mode (background)
docker run -it image bash Run interactive with terminal
docker run --name myapp image Run with custom name
docker run -p 8080:80 image Map port 8080 (host) to 80 (container)
docker run -v /host:/container image Bind mount volume
docker run --rm image Auto-remove on exit
docker run -e KEY=VALUE image Set environment variable
docker run --restart=always image Auto-restart policy
docker run --network=mynet image Connect to network
docker ps List running containers
docker ps -a List all containers
docker start container Start stopped container
docker stop container Stop running container
docker restart container Restart container
docker rm container Remove container
docker rm -f container Force remove running container
docker rm $(docker ps -aq) Remove all containers
docker rename old new Rename container

Container Inspection

Command Description
docker inspect container Show container details (JSON)
docker inspect --format '{{.State.Status}}' container Get specific field
docker port container Show port mappings
docker top container Show running processes
docker stats Live resource usage (all containers)
docker stats container Live resource usage (single)
docker diff container Show filesystem changes
docker cp container:/path /host/path Copy file from container
docker cp /host/path container:/path Copy file to container

Exec & Attach

Command Description
docker exec -it container bash Open bash shell in container
docker exec -it container sh Open sh shell (Alpine)
docker exec container command Run command in container
docker exec -u root container command Run as root
docker exec -e VAR=value container command Run with env var
docker attach container Attach to running container
docker wait container Wait for container to stop

Volumes

Command Description
docker volume create myvolume Create named volume
docker volume ls List all volumes
docker volume inspect myvolume Show volume details
docker volume rm myvolume Remove volume
docker volume prune Remove all unused volumes
docker run -v myvolume:/data image Mount named volume
docker run -v /host/path:/container/path image Bind mount
docker run -v /host/path:/container/path:ro image Read-only bind mount
docker run --tmpfs /tmp image Mount tmpfs (in-memory)

Networks

Command Description
docker network create mynet Create network
docker network create --driver bridge mynet Create bridge network
docker network create --driver overlay mynet Create overlay network (Swarm)
docker network ls List all networks
docker network inspect mynet Show network details
docker network rm mynet Remove network
docker network prune Remove unused networks
docker network connect mynet container Connect container to network
docker network disconnect mynet container Disconnect from network
docker run --network=mynet image Run on specific network

Docker Compose

Command Description
docker compose up Start all services
docker compose up -d Start in detached mode
docker compose up --build Build and start
docker compose down Stop and remove containers
docker compose down -v Stop and remove including volumes
docker compose down --rmi all Stop and remove including images
docker compose ps List running services
docker compose logs Show logs of all services
docker compose logs -f service Follow logs of a service
docker compose exec service bash Shell into a service
docker compose build Build all services
docker compose pull Pull all service images
docker compose restart Restart all services
docker compose stop Stop all services
docker compose config Validate and show config
docker compose top Show running processes
docker compose run service command Run one-off command

Compose File Example

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    depends_on:
      - api
    restart: unless-stopped

  api:
    build: ./api
    environment:
      - DB_HOST=db
    networks:
      - backend

  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=secret
    networks:
      - backend

volumes:
  db-data:

networks:
  backend:

Dockerfile Reference

Instruction Description
FROM image:tag Base image
WORKDIR /app Set working directory
COPY . . Copy files into image
COPY --chown=node:node . . Copy with ownership
ADD archive.tar.gz /app Copy and extract archive
RUN command Execute command during build
RUN apt-get update && apt-get install -y pkg Install packages
CMD ["node", "server.js"] Default command (overridable)
ENTRYPOINT ["node"] Fixed entrypoint
EXPOSE 3000 Document exposed port
ENV KEY=value Set environment variable
ARG VERSION=latest Build-time variable
VOLUME /data Declare volume mount point
USER node Set runtime user
HEALTHCHECK CMD curl -f http://localhost/ Define health check
LABEL maintainer="name" Add metadata

Multi-Stage Build Example

# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]

Build

Command Description
docker build -t name:tag . Build image from Dockerfile
docker build -f Dockerfile.prod . Build with custom Dockerfile
docker build --no-cache . Build without cache
docker build --build-arg VERSION=1.0 . Pass build argument
docker build --target builder . Build specific stage
docker build --platform linux/amd64 . Build for specific platform
docker buildx build --platform linux/amd64,linux/arm64 . Multi-platform build

Registry & Push

Command Description
docker login Log in to Docker Hub
docker login registry.example.com Log in to custom registry
docker logout Log out
docker push user/image:tag Push image to registry
docker pull user/image:tag Pull image from registry
docker search keyword Search Docker Hub
docker tag local:tag user/image:tag Tag for registry

Cleanup

Command Description
docker system prune Remove unused data
docker system prune -a Remove all unused data
docker system prune -a --volumes Remove everything including volumes
docker system df Show disk usage
docker container prune Remove stopped containers
docker image prune Remove dangling images
docker image prune -a Remove all unused images
docker volume prune Remove unused volumes
docker network prune Remove unused networks
docker builder prune Remove build cache

Resource Limits

Command Description
docker run --memory=512m image Limit memory to 512 MB
docker run --memory=1g image Limit memory to 1 GB
docker run --cpus=1.5 image Limit to 1.5 CPU cores
docker run --cpu-shares=512 image Relative CPU weight
docker run --pids-limit=100 image Limit number of processes
docker run --memory-swap=1g image Limit memory + swap
docker update --memory=1g container Update limits on running container

Logs & Monitoring

Command Description
docker logs container Show container logs
docker logs -f container Follow logs in real time
docker logs --tail 100 container Show last 100 lines
docker logs --since 1h container Logs from last hour
docker logs --timestamps container Show timestamps
docker events Stream Docker daemon events
docker stats Live resource usage
docker system info Show system-wide info

Environment Variables

Command Description
docker run -e KEY=VALUE image Set single variable
docker run -e KEY image Pass from host environment
docker run --env-file .env image Load from env file
docker compose --env-file .env up Compose with env file

.env File Format

DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=secret

Useful Flags & Patterns

Pattern Description
docker run -d --restart=unless-stopped Run as persistent service
docker run --init image Use init process (PID 1 handling)
docker run --read-only image Read-only filesystem
docker run --security-opt no-new-privileges image Prevent privilege escalation
docker run -w /app image command Set working directory
docker run --hostname myhost image Set hostname
docker run --add-host host:ip image Add host entry
docker run --dns 8.8.8.8 image Set DNS server
docker ps --format "table {{.Names}}\t{{.Status}}" Custom output format
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container Get container IP