Skip to content

Docker Cheat Sheet

Last verified May 2026 — runs in your browser
Docker Cheatsheet
docker pull <image>

Download an image from registry

Images
docker images

List local images

Images
docker rmi <image>

Remove an image

Images
docker image prune -a

Remove all unused images

Images
docker build -t name:tag .

Build image from Dockerfile

Images
docker build --no-cache -t name .

Build without cache

Images
docker tag src:latest user/src:1.0

Tag an image

Images
docker push user/image:tag

Push image to registry

Images
docker history <image>

Show image layer history

Images
docker save -o backup.tar image

Save image to tar file

Images
docker load -i backup.tar

Load image from tar file

Images
docker run <image>

Run a container from image

Containers
docker run -d --name web nginx

Run detached with name

Containers
docker run -it ubuntu bash

Run interactive with terminal

Containers
docker run -p 8080:80 nginx

Map host port to container port

Containers
docker run -v /host:/container image

Mount host directory as volume

Containers
docker run --rm image

Auto-remove on exit

Containers
docker run -e KEY=value image

Set environment variable

Containers
docker run --env-file .env image

Load env from file

Containers
docker ps

List running containers

Containers
docker ps -a

List all containers (incl. stopped)

Containers
docker stop <container>

Stop a running container

Containers
docker start <container>

Start a stopped container

Containers
docker restart <container>

Restart container

Containers
docker rm <container>

Remove a stopped container

Containers
docker rm -f <container>

Force-remove running container

Containers
docker container prune

Remove all stopped containers

Containers
docker exec -it <container> bash

Open shell in container

Containers
docker logs <container>

Show container logs

Containers
docker logs -f --tail 100 <container>

Follow last 100 log lines

Containers
docker inspect <container>

Show detailed info (JSON)

Containers
docker stats

Live resource usage per container

Containers
docker cp file container:/path

Copy file into container

Containers
FROM node:20-alpine

Base image

Dockerfile
WORKDIR /app

Set working directory

Dockerfile
COPY . .

Copy files into image

Dockerfile
COPY package*.json ./

Copy matching pattern

Dockerfile
RUN npm install

Run command at build time

Dockerfile
ENV NODE_ENV=production

Set environment variable

Dockerfile
EXPOSE 3000

Document listening port

Dockerfile
CMD ["node", "server.js"]

Default container command

Dockerfile
ENTRYPOINT ["/app/start.sh"]

Fixed entrypoint command

Dockerfile
ARG VERSION=1.0

Build-time argument

Dockerfile
USER node

Switch user (security)

Dockerfile
HEALTHCHECK CMD curl -f http://localhost/ || exit 1

Container healthcheck

Dockerfile
docker compose up

Start services from compose file

Compose
docker compose up -d

Start detached

Compose
docker compose down

Stop and remove services

Compose
docker compose down -v

Stop and remove volumes too

Compose
docker compose ps

List compose services

Compose
docker compose logs -f service

Follow logs for one service

Compose
docker compose build

(Re)build all services

Compose
docker compose exec service bash

Shell into a compose service

Compose
docker compose restart service

Restart one service

Compose
docker network ls

List networks

Networks
docker network create mynet

Create a network

Networks
docker run --network mynet image

Attach container to network

Networks
docker volume ls

List volumes

Networks
docker volume create mydata

Create named volume

Networks
docker volume prune

Remove unused volumes

Networks
docker system prune -a

Remove all unused data (careful)

Networks
Showing 61 of 61 commands

Docker Cheat Sheet — Containers, Images, Compose, Volumes & Networks

Docker shipped in 2013 and turned containers from a Linux kernel curiosity into the default way to package server software. The cheatsheet below covers 60+ commands across images, containers, Dockerfile instructions, Compose, networks, and volumes. Most trouble in everyday Docker work does not come from forgetting commands. It comes from quirks that look ordinary until they bite. A `COPY . .` placed before `RUN npm install` invalidates the cached install layer on every source-file change, turning a five-second rebuild into a five-minute one — the fix is to `COPY package*.json ./` and run install before copying the rest. A bind mount with `-v /host:/container` completely hides whatever the image baked into that path, which is why a freshly built image suddenly looks empty in dev. `docker system prune -a` removes every image not currently used by a running container, not just dangling ones, so a stopped container's image vanishes with it. These are the snippets pulled up while debugging exactly that — the right `COPY` order, the right Compose flag, the right log tail — without rereading the Docker docs from page one each time.

Common pitfalls in Docker

A few patterns earn their place near the top of any Dockerfile. Order instructions from least to most frequently changing so the build cache stays warm: base image, then OS packages, then dependency manifests (`package*.json`, `requirements.txt`, `go.mod`), then `RUN install`, and only after that the application source. `CMD` and `ENTRYPOINT` look similar but differ at runtime — `CMD` is the default command and gets fully replaced by anything passed after `docker run image`, while `ENTRYPOINT` is fixed and the runtime arguments become its parameters, which is what most production images actually want. A `.dockerignore` file in the build context is what stops `node_modules`, `.git`, and build artefacts from being copied into every layer; without it, the image bloats and cache invalidation gets unpredictable. `EXPOSE` documents the listening port but does not publish it — only `-p` or Compose `ports:` makes the port reachable from the host. The cheatsheet groups all of this into Images, Containers, Dockerfile, Compose, and Networks so the right section is one click away.

  • 60+ Docker commands organized by workflow
  • Image management (pull, build, push, tag)
  • Container lifecycle (run, exec, logs, stats)
  • Dockerfile instruction reference
  • docker compose commands
  • Networks and volumes

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).

By ·