Menu
CoddyTech

Docker Cheat Sheet

Last updated

Images

An image is the read-only template a container starts from.

What it doesCommand
Download an imagedocker pull nginx
Download a specific tagdocker pull node:22-alpine
List local imagesdocker images
List image IDs onlydocker images -q
Build an image from the Dockerfile heredocker build -t myapp .
Add another name and tag to an imagedocker tag myapp myuser/myapp:1.0
Show the layers an image was built fromdocker history myapp
Show an image's full details as JSONdocker image inspect myapp
Remove an imagedocker rmi myapp
Remove images no container usesdocker image prune -a
Search Docker Hubdocker search redis
Save an image to a tar filedocker save -o myapp.tar myapp
Load an image from a tar filedocker load -i myapp.tar

Containers

A container is a running (or stopped) instance of an image.

What it doesCommand
Start a container from an imagedocker run nginx
Start one in the background with a namedocker run -d --name web nginx
List running containersdocker ps
List all containers, stopped ones toodocker ps -a
Stop a containerdocker stop web
Start a stopped containerdocker start web
Restart a containerdocker restart web
Remove a stopped containerdocker rm web
Stop and remove in one stepdocker rm -f web
Open a shell in a running containerdocker exec -it web sh
Run one command in a running containerdocker exec web ls /usr/share/nginx/html
Show a container's logsdocker logs web
Follow the logs as they comedocker logs -f web
Show the last 100 lines of the logsdocker logs --tail 100 web
Copy a file out of a containerdocker cp web:/etc/nginx/nginx.conf .
Copy a file into a containerdocker cp ./index.html web:/usr/share/nginx/html/
Show the processes running in a containerdocker top web
Show live CPU and memory usedocker stats
Show which host ports map to a containerdocker port web
Rename a containerdocker rename web web-old

docker run options

The flags you combine on docker run. Options go before the image name; anything after it is the command to run.

What it doesCommand
Run in the background (detached)docker run -d nginx
Give the container a namedocker run --name web nginx
Publish port 80 as port 8080 on the hostdocker run -p 8080:80 nginx
Publish every exposed port on a random host portdocker run -P nginx
Open an interactive shelldocker run -it ubuntu bash
Remove the container when it exitsdocker run --rm alpine echo hello
Set an environment variabledocker run -e NODE_ENV=production myapp
Read environment variables from a filedocker run --env-file .env myapp
Mount a named volumedocker run -v data:/var/lib/postgresql/data postgres
Mount the current folder (bind mount)docker run -v "$(pwd)":/app myapp
Mount a folder read-onlydocker run -v "$(pwd)":/app:ro myapp
Set the working directorydocker run -w /app node:22 npm test
Join a networkdocker run --network mynet myapp
Restart unless stopped by handdocker run -d --restart unless-stopped nginx
Limit memory and CPUdocker run -m 512m --cpus 1.5 myapp
Override the entrypointdocker run --entrypoint sh -it myapp
Run as another userdocker run -u 1000:1000 myapp

Building images

What it doesCommand
Build and tag from the Dockerfile heredocker build -t myapp:1.0 .
Build with a Dockerfile under another namedocker build -f Dockerfile.dev -t myapp:dev .
Build without the layer cachedocker build --no-cache -t myapp .
Pass a build argument (an ARG)docker build --build-arg VERSION=1.2 -t myapp .
Build only one stage of a multi-stage filedocker build --target build -t myapp:build .
Build for another CPU architecturedocker build --platform linux/amd64 -t myapp .
Show every step's full outputdocker build --progress=plain -t myapp .
Build for two architectures and pushdocker buildx build --platform linux/amd64,linux/arm64 -t myuser/myapp:1.0 --push .

Dockerfile instructions

A Dockerfile lists the steps that build an image, one instruction per line. Files to leave out of the build go in a .dockerignore file.

What it doesInstruction
The base image to start fromFROM node:22-alpine
Name a stage of a multi-stage buildFROM node:22 AS build
Set the working directory for later stepsWORKDIR /app
Copy files from the build contextCOPY package*.json ./
Copy files from an earlier stageCOPY --from=build /app/dist ./dist
Copy, also unpacking archives and fetching URLsADD app.tar.gz /opt/
Run a command while buildingRUN npm ci
Set an environment variableENV NODE_ENV=production
Declare a build argumentARG VERSION=1.0
Document the port the app listens onEXPOSE 3000
The default command when the container startsCMD ["node", "server.js"]
The fixed program to run (CMD becomes its arguments)ENTRYPOINT ["python", "app.py"]
Run the next steps as another userUSER node
Mark a folder as a volumeVOLUME /data
Check that the app is healthyHEALTHCHECK CMD curl -f http://localhost:3000/ || exit 1
Add metadataLABEL org.opencontainers.image.source="https://github.com/me/myapp"

Volumes

Volumes keep data when a container is removed.

What it doesCommand
Create a volumedocker volume create data
List volumesdocker volume ls
Show where a volume livesdocker volume inspect data
Remove a volumedocker volume rm data
Remove volumes no container usesdocker volume prune

Networks

Containers on the same user-defined network reach each other by container name.

What it doesCommand
Create a networkdocker network create mynet
List networksdocker network ls
Show a network and its containersdocker network inspect mynet
Connect a running containerdocker network connect mynet web
Disconnect a containerdocker network disconnect mynet web
Remove a networkdocker network rm mynet
Remove networks no container usesdocker network prune

Docker Compose

Compose runs several containers described in a compose.yaml (or docker-compose.yml) file in the current folder.

What it doesCommand
Start every service in the backgrounddocker compose up -d
Rebuild the images, then startdocker compose up -d --build
Stop and remove the containers and networksdocker compose down
Also remove the volumesdocker compose down -v
List the project's containersdocker compose ps
Follow the logs of every servicedocker compose logs -f
Follow one service's logsdocker compose logs -f web
Open a shell in a running servicedocker compose exec web sh
Run a one-off command in a new containerdocker compose run --rm web npm test
Build or rebuild the imagesdocker compose build
Pull the latest imagesdocker compose pull
Restart the servicesdocker compose restart
Stop without removingdocker compose stop
Show the merged, final configurationdocker compose config
Use another filedocker compose -f compose.prod.yaml up -d

Registries

Share images through Docker Hub or another registry.

What it doesCommand
Log in to Docker Hubdocker login
Log in to another registrydocker login ghcr.io
Tag an image with your account namedocker tag myapp myuser/myapp:1.0
Upload an imagedocker push myuser/myapp:1.0
Log outdocker logout

Inspecting and debugging

What it doesCommand
Show a container's full details as JSONdocker inspect web
Print one field from the detailsdocker inspect -f '{{.State.Status}}' web
Print a container's IP addressdocker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
Show the files changed inside a containerdocker diff web
Take one snapshot of resource usedocker stats --no-stream
Watch events from the Docker daemondocker events
Show client and server versionsdocker version
Show system-wide informationdocker info

Cleanup

Docker keeps stopped containers, old images and build cache until you remove them.

What it doesCommand
Show disk space used by Dockerdocker system df
Remove stopped containersdocker container prune
Remove dangling imagesdocker image prune
Remove every unused imagedocker image prune -a
Remove the build cachedocker builder prune
Remove stopped containers, unused networks, dangling images and cachedocker system prune
Remove everything unused, volumes includeddocker system prune -a --volumes
Stop every running containerdocker stop $(docker ps -q)
Remove every containerdocker rm -f $(docker ps -aq)

Every Docker command you reach for, in copy-paste tables: images and containers first, then the docker run flags, building, Dockerfile instructions, volumes, networks, Docker Compose, registries and cleanup.

The commands are for the current Docker CLI, where Compose is the docker compose plugin (older installs use docker-compose with a hyphen). They work the same on Linux, macOS and Windows. To try them without installing anything, open the terminal playground, which has a Docker simulator built in.

Docker cheat sheet FAQ

What is the difference between an image and a container?
An image is a read-only template: a filesystem plus the command to run. A container is a running instance of an image, with its own writable layer on top. docker build and docker pull give you images; docker run starts a container from one. You can run many containers from the same image.
What is the difference between CMD and ENTRYPOINT?
CMD is the default command, and anything you type after the image name in docker run replaces it. ENTRYPOINT is the fixed program, and docker run arguments are added to it instead (CMD then supplies its default arguments). Use CMD for most apps and ENTRYPOINT when the image is a single tool.
What is the difference between COPY and ADD?
Both copy files into the image. ADD also unpacks a local tar archive and can download a URL. Use COPY unless you need one of those, because its behaviour is plainer.
Is it docker compose or docker-compose?
docker compose (with a space) is Compose V2, a plugin of the Docker CLI and the one installed today. docker-compose (with a hyphen) is the older standalone program, no longer updated. The subcommands are the same: up, down, ps, logs, exec.
How do I remove all stopped containers and unused images?
docker system prune removes stopped containers, unused networks, dangling images and the build cache. Add -a to also remove every image no container uses, and --volumes to remove unused volumes, which deletes their data.
How do I get a shell inside a container?
For a running container, docker exec -it web sh (or bash if the image has it). To start a new container with a shell instead, docker run -it --rm ubuntu bash.
Can I practice Docker commands online?
Yes. The terminal playground has a Docker simulator: docker build, docker run, docker ps, volumes, networks and more answer as the real CLI does, with nothing to install. It runs a set of common tools inside containers rather than each image's own programs, so for a full Docker install use Docker Desktop or Docker Engine on your machine.
Is this Docker cheat sheet free?

Yes. It is free to read, copy and print, with no sign-up.

Coddy programming languages illustration

Learn Docker with Coddy

Get started