All cheatsheets

Cheatsheets

Docker

Everyday Docker & Compose commands for containers, images, and cleanup.

31 entries

Containers9

docker run -d -p 8080:80 nginx

Run detached, mapping a port

docker run -it ubuntu bash

Run interactively with a shell

docker run --rm -v $PWD:/app -w /app node npm ci

Throwaway container, mount cwd

docker ps

List running containers

docker ps -a

List all containers (incl. stopped)

docker exec -it <id> sh

Open a shell in a running container

docker logs -f --tail 100 <id>

Follow the last 100 log lines

docker stop <id> && docker rm <id>

Stop and remove a container

docker cp <id>:/path ./local

Copy files out of a container

Images7

docker build -t name:tag .

Build an image from the Dockerfile

docker build --no-cache .

Build without using the cache

docker images

List local images

docker pull <image>

Download an image from a registry

docker tag <src> <target>

Add a tag to an image

docker push <image>

Upload an image to a registry

docker history <image>

Show an image's layer history

Dockerfile5

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

Minimal Node image (cache deps before source)

FROM x AS build

Name a stage for multi-stage builds

COPY --from=build /app/dist ./dist

Copy artifacts from another stage

ARG VERSION / ENV NODE_ENV=production

Build-time arg vs runtime env

HEALTHCHECK CMD curl -f localhost/ || exit 1

Container health probe

Compose5

docker compose up -d

Start services in the background

docker compose down -v

Stop services and remove volumes

docker compose logs -f <svc>

Follow a service's logs

docker compose exec <svc> sh

Shell into a running service

services: web: build: . ports: ["3000:3000"] depends_on: [db] db: image: postgres:16 environment: POSTGRES_PASSWORD: secret

compose.yaml: web + postgres

System & cleanup5

docker system df

Show Docker disk usage

docker system prune -a

Remove unused images, networks, build cache

docker volume prune

Remove unused volumes

docker stats

Live resource usage of containers

docker inspect <id>

Low-level details as JSON