cluster deboistech.in Get started
Cheatsheet

Docker

The Docker commands and Dockerfile lines for images, containers, storage, networks and Compose.

Practice this path

Run containers

  • docker run -d --name web -p 8080:80 nginx:1.27

    Start a container in the background, publishing host port 8080 to container port 80

  • docker run --rm -it ubuntu:24.04 bash

    A throwaway interactive shell (removed on exit)

  • docker run -e KEY=value --env-file .env myapp

    Pass environment variables

  • docker run --memory 256m --cpus 0.5 myapp

    Limit memory and CPU

  • docker run --restart unless-stopped myapp

    Restart it after a crash or reboot

  • docker run -v "$PWD":/app -w /app node:20 npm test

    Run a command in a container against the current folder

Inspect and control

  • docker ps -a

    List containers, including stopped ones

  • docker logs -f --tail 100 web

    Follow the last 100 lines of a container's output

  • docker exec -it web sh

    Open a shell inside a running container

  • docker inspect web

    Everything about it as JSON (add --format '{{.State.Status}}' for one field)

  • docker stats --no-stream

    CPU and memory use right now

  • docker stop web && docker rm web

    Stop and delete a container

  • docker cp web:/etc/nginx/nginx.conf .

    Copy a file out of a container

  • docker top web

    The processes running inside it

Images

  • docker build -t myapp:1.0 .

    Build an image from the Dockerfile here

  • docker build --no-cache -t myapp:1.0 .

    Rebuild without reusing cached layers

  • docker images

    List local images

  • docker history myapp:1.0

    The layers of an image and their sizes

  • docker tag myapp:1.0 registry.example.com/team/myapp:1.0

    Give an image another name

  • docker push registry.example.com/team/myapp:1.0

    Upload an image to a registry (docker login first)

  • docker pull nginx:1.27

    Download an image

  • docker save -o app.tar myapp:1.0  |  docker load -i app.tar

    Move an image as a file

Dockerfile

Each instruction is a layer. Put what changes least first, so the cache helps.

  • FROM node:20-slim

    The base image: pin a version and prefer small ones

  • WORKDIR /app

    Set the folder for the following instructions

  • COPY package*.json ./
    RUN npm ci --omit=dev
    COPY . .

    Install dependencies before copying the code, so they cache

  • ENV NODE_ENV=production

    Set an environment variable

  • EXPOSE 3000

    Document the port the app listens on

  • USER node

    Do not run as root

  • HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1

    Let Docker know when the app is healthy

  • ENTRYPOINT ["node"]
    CMD ["server.js"]

    ENTRYPOINT is the program; CMD is its default arguments

  • FROM golang:1.22 AS build
    ...
    FROM gcr.io/distroless/static
    COPY --from=build /out/app /app

    A multi-stage build: ship only the result, not the compiler

  • .dockerignore:  .git  node_modules  *.log

    Keep files out of the build context

Volumes and networks

  • docker volume create data

    Make a named volume that outlives containers

  • docker run -v data:/var/lib/postgresql/data postgres:16

    Mount a volume for a database

  • docker run -v "$PWD/conf":/etc/app:ro myapp

    Bind-mount a folder read-only

  • docker network create backend

    Make a network; containers on it reach each other by name

  • docker run --network backend --name db postgres:16

    Attach a container to it

  • docker network inspect backend

    See who is connected

Compose

  • docker compose up -d --build

    Build and start all services in the background

  • docker compose ps  |  docker compose logs -f web

    Status and logs of the stack

  • docker compose exec web sh

    A shell in a service

  • docker compose down -v

    Stop everything and remove the volumes too

  • services:
      web:
        build: .
        ports: ["8080:3000"]
        depends_on: [db]
      db:
        image: postgres:16
        volumes: [pgdata:/var/lib/postgresql/data]
    volumes:
      pgdata:

    A compose.yaml with an app and a database

Clean up

  • docker system df

    How much space images, containers and volumes take

  • docker image prune -a

    Delete images no container uses

  • docker container prune

    Delete all stopped containers

  • docker system prune --volumes

    Delete unused containers, networks, images and volumes (check first)

Cheatsheet for the Docker path · Deboistech · All cheatsheets