Skip to content

Docker cheat sheet

The Docker and Docker Compose commands for images, containers, volumes, networks and multi-stage builds that the containers course uses.

Containers

CommandWhat it does
docker run -d --name web -p 8080:80 nginxrun in the background, publish a port
docker run --rm -it ubuntu basha throwaway interactive shell
docker ps / docker ps -arunning / all containers
docker logs -f webfollow output
docker exec -it web sha shell inside a running container
docker stop web / docker rm webstop / remove
docker inspect webeverything about it, as JSON
docker statslive CPU and memory

Images

CommandWhat it does
docker build -t app:1.0 .build from the Dockerfile here
docker build --target build .build one stage only
docker images / docker history app:1.0list / size per layer
docker tag app:1.0 registry/app:1.0 / docker push …name / upload
docker image pruneremove dangling images

Dockerfile

FROM golang:1.23 AS build      # a named build stage
WORKDIR /src
COPY go.mod go.sum ./          # dependency files first, for the cache
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .

FROM alpine:3.20               # the image you ship
RUN adduser -D app
USER app
COPY --from=build /out/app /usr/local/bin/app
EXPOSE 8080
ENTRYPOINT ["app"]

CMD is the default command and can be overridden; ENTRYPOINT is the program. Add a .dockerignore.

Volumes and networks

CommandWhat it does
docker volume create dataa named volume
-v data:/var/lib/postgresql/datamount a volume
-v "$PWD":/appbind-mount a host folder
docker network create appneta user-defined network
--network appnetcontainers here find each other by name

Compose

CommandWhat it does
docker compose up -dstart the stack
docker compose ps / logs -f apistatus / one service's log
docker compose down / down -vstop / and delete volumes
docker compose build / up -d --buildrebuild images
services:
  api:
    build: .
    ports: ["8000:8000"]
    environment: { DATABASE_URL: postgres://app:secret@db/app }
    depends_on: { db: { condition: service_healthy } }
  db:
    image: postgres:16
    environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: secret }
    volumes: [db:/var/lib/postgresql/data]
    healthcheck: { test: ["CMD", "pg_isready", "-U", "app"], interval: 5s }
volumes: { db: {} }
Practise it on a real machineProduction Containerization course
Open the lab