Cheat Sheet Library

Docker & Docker Compose Cheat Sheet

Image = a blueprint. Container = a running instance of an image. Volume = persistent data. Compose = several containers defined in one YAML file.

  • 46 entries
  • 8 sections
  • Updated
  • Reviewed
  • Click a command to see all its options

46 entries

On this page

Containers #

CommandAction
Run in the background, host port 8080 → container port 80
Interactive shell in a new container
Delete the container when it stops
Set an environment variable
Load variables from a file
Mount a named volume
Mount the current folder (bind mount)
Restart automatically
/ Running / all containers
/ Stop / start
Restart
/ Remove / force-remove a running one
Open a shell in a running container
Follow logs
Copy a file out (or in)
Live CPU / RAM per container
All details (IP, mounts, env)

Images #

CommandAction
List images
Download an image
Build from the Dockerfile in this folder
Use a different Dockerfile
Tag for a registry
Upload to a registry
Remove an image
Layers and their sizes

Volumes & Networks #

CommandAction
List volumes
Create
Remove (the data is gone!)
List networks
Create a network
Containers on the same network reach each other by name (http://api:3000)

Cleanup #

CommandAction
Remove stopped containers
Remove dangling images
Remove all unused images
Remove everything unused (add --volumes for volumes, careful)
Disk usage

Docker Compose #

CommandAction
Start all services in the background
Rebuild images, then start
Stop and remove containers + network
…also remove volumes (deletes data!)
Service status
Follow the logs of one service
Shell into a service
Restart one service
Pull newer images
Show the final merged config

Example Dockerfile (Node) #

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

Add a .dockerignore with node_modules, .git and .env.

Example compose.yaml #

services:
  api:
    build: .
    ports:
      - "3000:3000"
    env_file: .env
    depends_on:
      - db
  db:
    image: mongo:7
    volumes:
      - dbdata:/data/db
volumes:
  dbdata:

Tips #

  • localhost inside a container is the container itself. Use the service name (db) to reach another container.
  • On Windows/macOS, host.docker.internal reaches your host machine.