Guide · Docker · about 6 minutes

Docker cheatsheet: commands by task

Every Docker command from the Docker basics track, grouped by the job you're trying to do, with what Docker's common errors mean, the Dockerfile and Compose basics, and how each idea maps to Kubernetes. Each row links to the lesson that explains it.

Last checked October 2026

How to use this page

Find the job you're trying to do, and copy the command. Words in capitals, like NAME, IMAGE or FILE, are yours to replace. IMAGE is a name and a tag, like nginx:1.28. Every row links to the lesson that explains it. Use the print button at the top to keep a copy next to you.

Two habits that make every command here easier:

  • When a container misbehaves, run docker ps -a, then docker logs NAME. The first shows its exit code, the second shows what it said before it stopped. Both work after it has stopped.
  • Pin your tags. nginx:1.28 means the same thing next month. nginx:latest might not.

When a container won't work

Start here when something's wrong. Find what you're seeing, and it tells you where to look.

You seeIt usually meansDo thisLesson
Exited (1) or any number but 0 in docker ps -aThe program inside failed and stopped. 0 means it finished finedocker logs NAME2
Exit code 137It used more memory than its limit, and was killed (OOM: out of memory)docker inspect NAME --format '{{.State.OOMKilled}}'. If true, give it more memory or make it use less5
Conflict. The container name "/NAME" is already in useNames must be unique, even among stopped containersdocker rm -f NAME, or pick another name2
Port is already allocatedSomething else is using that laptop portChange the first number: -p 8081:802
manifest unknown or manifest for IMAGE not foundThere's no image with that tagCheck the name and tag on the registry3
exec format errorThe image was built for a different processor, usually arm64 (an M-series Mac) on an amd64 serverRebuild with --platform linux/amd643, 8
Connection refused from one container to anotherThe address exists, but nothing is listening. Inside a container, localhost is the container itselfUse the other container's name, like cache, and check the port6
Name or service not knownThat name can't be found on the network: misspelled, or not on the same networkCheck the spelling and --network6
(unhealthy) in docker psIts HEALTHCHECK keeps failingdocker inspect NAME, then look under Health7
A setting seems to be ignoredOften a misspelled variable name. The app quietly uses its defaultdocker exec NAME env and check the spelling5

Running containers

ToRunLesson
Check Docker is workingdocker version1
Run a test containerdocker run hello-world2
Run in the background, with a name and a portdocker run -d -p 8080:80 --name NAME IMAGE2
Run once, with a shell, and remove it afterdocker run --rm -it IMAGE sh2
List running containersdocker ps2
List all containers, including stopped onesdocker ps -a2
Stop onedocker stop NAME2
Start a stopped one againdocker start NAME2
Remove one, even if it's runningdocker rm -f NAME2

In -p 8080:80, the first number is your laptop's port and the second is the container's. Only the first has to be free.

Inside a running container

ToRunLesson
Its logsdocker logs NAME2
Keep watching its logs (Ctrl+C to stop)docker logs -f NAME2
Run one command inside itdocker exec NAME cat /etc/os-release2
Open a shell inside it (exit to leave)docker exec -it NAME sh2
See its settingsdocker exec NAME env5
Memory and CPU each container is usingdocker stats --no-stream5
Everything Docker knows about itdocker inspect NAME5
Which user it runs asdocker run --rm IMAGE whoami7

Changes made inside a container disappear when it's removed. Anything worth keeping goes in a volume, or in the image.

Images and registries

An image name reads registry/owner/name:tag. nginx:1.28 is short for docker.io/library/nginx:1.28: Docker Hub's official image.

ToRunLesson
Download an imagedocker pull IMAGE3
List the images you havedocker image ls3
See its layers, and what made each onedocker image history IMAGE3
Which processor it's fordocker image inspect IMAGE --format '{{.Os}}/{{.Architecture}}'3
Which processors a registry image supportsdocker buildx imagetools inspect IMAGE3
Its digest: an ID that never changes, unlike a tagdocker image inspect IMAGE --format '{{index .RepoDigests 0}}'3
Delete an imagedocker rmi IMAGE3

Building your own image

ToRunLesson
Build from the Dockerfile in this folderdocker build -t NAME:TAG .4
Build from a different Dockerfiledocker build -f FILE -t NAME:TAG .7
Build for an amd64 server, from any laptopdocker build --platform linux/amd64 -t NAME:TAG .8
See your image's sizedocker image ls NAME4
Dockerfile lineWhat it doesLesson
FROM python:3.13-slimThe image to start from4
WORKDIR /appThe folder later lines work in4
COPY requirements.txt .Copies files from your folder into the image4
RUN pip install ...Runs a command while building, and saves the result as a layer4
ENV KEY="value"Sets an environment variable for everything after it7
EXPOSE 8000A note that the app listens on 8000. It doesn't publish anything; -p does that4
USER appRuns everything after it, and the container, as that user instead of root7
HEALTHCHECK ... CMD ...A check Docker runs on a schedule to mark the container healthy or unhealthy7
CMD ["flask", "run"]The command that runs when the container starts4
FROM ... AS builder, then COPY --from=builderA multi-stage build: build in one stage, copy only the result into a clean one7

Order matters. Docker reuses every layer up to the first line that changed. Copy the list of libraries and install them first, then copy your code, so changing your code doesn't reinstall everything.

Settings, data and limits

ToRunLesson
Pass one settingdocker run -e GREETING=Bonjour IMAGE5
Pass settings from a filedocker run --env-file settings.env IMAGE5
Pass a setting from your own shell, without typing its valuedocker run -e DATABASE_URL IMAGE5
Make a named volumedocker volume create NAME5
Attach a volumedocker run -v NAME:/data IMAGE5
Share this folder with the container (a bind mount)docker run -v "$PWD":/app IMAGE5
List volumesdocker volume ls5
Delete a volume, and its datadocker volume rm NAME5
Limit memory and CPUdocker run --memory 128m --cpus 0.5 IMAGE5
The exit code of the last command you ranecho $?5
Data written toLasts untilGood for
The container itselfThe container is removedNothing you want to keep
A named volumeYou delete the volumeDatabases, caches, anything the app saves
A bind mountYou delete the files: it's your own folderDevelopment: edit code on your laptop, run it in the container

Networks and Compose

Containers on the same network find each other by name. Compose puts every service in its file on one network for you.

ToRunLesson
Make a networkdocker network create NAME6
Run a container on itdocker run -d --name cache --network NAME IMAGE6
Delete a networkdocker network rm NAME6
Build and start everything in compose.yamldocker compose up -d --build6
Apply changes to compose.yamldocker compose up -d6
See its containersdocker compose ps6
One service's logsdocker compose logs web6
Stop and remove it all, keeping volumesdocker compose down6
Stop and remove it all, volumes toodocker compose down -v6

A small compose.yaml: a web app you build, and Redis from an existing image, with its data in a volume.

YAML
services:
  web:
    build: .
    ports:
      - "8000:8000"
    environment:
      REDIS_HOST: cache
    depends_on:
      - cache
  cache:
    image: redis:7.2.7-alpine
    volumes:
      - cache-data:/data
volumes:
  cache-data:

Smaller, safer images

DoWhyLesson
Start from a -slim or -alpine baseLess software to download, and less for a flaw to hide in7
Add a .dockerignoreKeeps .git, .env and other secrets out of the build7
Use a multi-stage buildCompilers and build tools stay behind in the first stage7
Run as a normal user, with a fixed numberuseradd --uid 1000 and USER app. Kubernetes can only check a number is not root7, 8
Copy only what the app needsCOPY . . copies everything, secrets included, unless .dockerignore stops it7
Scan the image before you ship itTrivy or Docker Scout list known flaws in what's inside7
Health checks look at the app onlyA check that depends on a database marks a good app unhealthy when the database is down7

A starting .dockerignore:

TEXT
.git
.env
*.env
__pycache__
.venv
compose.yaml

Shipping to a registry

ToRunLesson
Sign in to Docker Hubdocker login8
Give your image a registry namedocker tag NAME:TAG "$DOCKER_ID/NAME:TAG"8
Push itdocker push "$DOCKER_ID/NAME:TAG"8
Set up a builder that can build for several processors (once)docker buildx create --name multi --use8
Build for amd64 and arm64, and pushdocker buildx build --platform linux/amd64,linux/arm64 -t "$DOCKER_ID/NAME:TAG" --push .8
Load a local image into a kind cluster, no registry neededkind load docker-image NAME:TAG --name learn8

Cleaning up

ToRunLesson
See what's using disk spacedocker system df3
Remove stopped containers, unused networks and dangling images (it asks first)docker system prune3
Remove several containers at oncedocker rm -f NAME1 NAME22
Remove several images at oncedocker rmi IMAGE1 IMAGE24

Every Docker idea, in Kubernetes

When you move to Kubernetes, the Kubernetes cheatsheet picks up from here.

In DockerIn Kubernetes
docker run one containerA Pod, usually kept running by a Deployment
compose.yamlKubernetes YAML files, applied with kubectl apply
-p 8000:8000A Service, plus kubectl port-forward for testing
Service names on a Compose networkService names, through the cluster's DNS
-e, --env-fileenv, ConfigMaps and Secrets
A named volumeA PersistentVolumeClaim
--memory, --cpusRequests and limits
HEALTHCHECKReadiness and liveness probes
USERsecurityContext, such as runAsNonRoot