Skip to content

Docker Cheat Sheet

Site build October 2026 — calculations run locally

Docker Cheatsheet

Search code, tasks or categories in English or Spanish. Copy a snippet and adapt it to the stated context.

Showing 61 of 61 commands

Run Docker CLI commands in a terminal with Docker installed and its engine available. Replace <…> placeholders with your own values before running. Dockerfile instructions belong in a Dockerfile; Compose commands require a Compose project. Copying does not execute a command. Read the notes on commands that remove data.

Download an image from registry

Images · Pattern / syntax
docker pull <image>

List local images

Images · Pattern / syntax
docker images

Remove an image

Images · Pattern / syntax
docker rmi <image>

Remove images not used by any container

Images · Pattern / syntax
docker image prune -a

With -a, this includes unused tagged images, not only dangling images. Review before running; an image referenced by a container is kept.

Build image from Dockerfile

Images · Pattern / syntax
docker build -t name:tag .

Build without cache

Images · Pattern / syntax
docker build --no-cache -t name .

Tag an image

Images · Pattern / syntax
docker tag src:latest user/src:1.0

Push image to registry

Images · Pattern / syntax
docker push user/image:tag

Show image layer history

Images · Pattern / syntax
docker history <image>

Save image to tar file

Images · Pattern / syntax
docker save -o backup.tar image

Load image from tar file

Images · Pattern / syntax
docker load -i backup.tar

Run a container from image

Containers · Pattern / syntax
docker run <image>

Run detached with name

Containers · Pattern / syntax
docker run -d --name web nginx

Run interactive with terminal

Containers · Pattern / syntax
docker run -it ubuntu bash

Map host port to container port

Containers · Pattern / syntax
docker run -p 8080:80 nginx

Mount host directory as volume

Containers · Pattern / syntax
docker run -v /host:/container image

Auto-remove on exit

Containers · Pattern / syntax
docker run --rm image

Set environment variable

Containers · Pattern / syntax
docker run -e KEY=value image

Load env from file

Containers · Pattern / syntax
docker run --env-file .env image

List running containers

Containers · Pattern / syntax
docker ps

List all containers (incl. stopped)

Containers · Pattern / syntax
docker ps -a

Stop a running container

Containers · Pattern / syntax
docker stop <container>

Start a stopped container

Containers · Pattern / syntax
docker start <container>

Restart container

Containers · Pattern / syntax
docker restart <container>

Remove a stopped container

Containers · Pattern / syntax
docker rm <container>

Force-remove running container

Containers · Pattern / syntax
docker rm -f <container>

Remove all stopped containers

Containers · Pattern / syntax
docker container prune

Open shell in container

Containers · Pattern / syntax
docker exec -it <container> bash

Show container logs

Containers · Pattern / syntax
docker logs <container>

Follow last 100 log lines

Containers · Pattern / syntax
docker logs -f --tail 100 <container>

Show detailed info (JSON)

Containers · Pattern / syntax
docker inspect <container>

Live resource usage per container

Containers · Pattern / syntax
docker stats

Copy file into container

Containers · Pattern / syntax
docker cp file container:/path

Base image

Dockerfile · Pattern / syntax
FROM node:24-alpine

Node.js 24 is an official supported LTS image line. Alpine uses musl libc; check compatibility if your dependencies include native modules.

Set working directory

Dockerfile · Pattern / syntax
WORKDIR /app

Copy files into image

Dockerfile · Pattern / syntax
COPY . .

Copy matching pattern

Dockerfile · Pattern / syntax
COPY package*.json ./

Run command at build time

Dockerfile · Pattern / syntax
RUN npm install

When package-lock.json is committed, prefer npm ci for a clean install from the lockfile; keep package.json and the lockfile in sync.

Set environment variable

Dockerfile · Pattern / syntax
ENV NODE_ENV=production

Document listening port

Dockerfile · Pattern / syntax
EXPOSE 3000

Default container command

Dockerfile · Pattern / syntax
CMD ["node", "server.js"]

Fixed entrypoint command

Dockerfile · Pattern / syntax
ENTRYPOINT ["/app/start.sh"]

This is exec form: docker run arguments are passed to the entrypoint. Shell form behaves differently, ignores CMD/run arguments, and can prevent the process receiving stop signals.

Build-time argument

Dockerfile · Pattern / syntax
ARG VERSION=1.0

Switch user (security)

Dockerfile · Pattern / syntax
USER node

Container healthcheck

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

Only works if curl exists in the image and the app serves this URL. Use a health endpoint and a probe available in your image.

Start services from compose file

Compose · Pattern / syntax
docker compose up

Start detached

Compose · Pattern / syntax
docker compose up -d

Stop and remove services

Compose · Pattern / syntax
docker compose down

Remove project volumes as well as containers

Compose · Pattern / syntax
docker compose down -v

This removes named volumes declared by the project and attached anonymous volumes, deleting their data. External volumes are kept. Check the Compose file and back up needed data first.

List compose services

Compose · Pattern / syntax
docker compose ps

Follow logs for one service

Compose · Pattern / syntax
docker compose logs -f service

(Re)build all services

Compose · Pattern / syntax
docker compose build

Shell into a compose service

Compose · Pattern / syntax
docker compose exec service bash

Restart one service

Compose · Pattern / syntax
docker compose restart service

List networks

Networks · Pattern / syntax
docker network ls

Create a network

Networks · Pattern / syntax
docker network create mynet

Attach container to network

Networks · Pattern / syntax
docker run --network mynet image

List volumes

Networks · Pattern / syntax
docker volume ls

Create named volume

Networks · Pattern / syntax
docker volume create mydata

Remove unused anonymous volumes by default

Networks · Pattern / syntax
docker volume prune

By default, removes unreferenced anonymous local volumes. Add -a to include named volumes; either can contain data you still need.

Remove stopped containers, unused networks/images and build cache

Networks · Pattern / syntax
docker system prune -a

-a includes all unused images, not only dangling ones. This also removes stopped containers, unused networks and build cache. Volumes are not included unless --volumes is added (then unused anonymous volumes are pruned). Review before running.

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

Docker launched in 2013, and its commands are now widely used to build and run container images. This cheatsheet covers 60+ commands across images, containers, Dockerfile instructions, Compose, networks, and volumes. A `COPY . .` before dependency installation can invalidate that layer whenever source files change, so Docker reruns the install and every later layer. Copy dependency manifests first, install dependencies, then copy application code. A bind mount such as `-v /host:/container` hides files already present at the container path while the mount is active. `docker system prune -a` removes stopped containers, unused networks, unused images (not only dangling images), and build cache; volumes are excluded unless `--volumes` is added, which prunes unused anonymous volumes. Check the cleanup scope before running it.

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 earlier layers can be reused: base image, system packages, dependency manifests (`package*.json`, `requirements.txt`, `go.mod`), the matching install step, then application source. For npm projects with a committed `package-lock.json`, `npm ci` installs from that lockfile. With exec-form `CMD`, the command and arguments are defaults that runtime arguments can replace. An exec-form `ENTRYPOINT` sets the executable and receives runtime arguments; shell form behaves differently and can prevent the process from receiving stop signals. A `.dockerignore` file keeps files such as `node_modules`, `.git`, and build artefacts out of the build context. `EXPOSE` documents a listening port but does not publish it — only `-p` or Compose `ports:` makes it reachable from the host. The cheatsheet groups these topics so the right section is easy to find.

  • 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 ·