Docker Cheat Sheet

Free Docker cheat sheet: the everyday docker and docker compose commands, Dockerfile instructions and image/container basics at a glance.

Images

ConceptSyntaxExample
Pull an image
Downloads an image from a registry like Docker Hub.
docker pull image:tagdocker pull nginx:latest
Build an image
-t tags the image; . is the build context (the Dockerfile dir).
docker build -t name .docker build -t myapp:1.0 .
List images
Shows local images with their sizes and tags.
docker imagesdocker images
Tag an image
Add a new name/tag, e.g. before pushing to a registry.
docker tag src targetdocker tag myapp:1.0 user/myapp:latest
Push an image
Uploads to a registry; log in first with docker login.
docker push name:tagdocker push user/myapp:latest

Containers

ConceptSyntaxExample
Run a container
-d detaches (background); -p maps host:container ports.
docker run imagedocker run -d -p 8080:80 nginx
Run interactively
-it gives an interactive terminal inside the container.
docker run -it image shdocker run -it ubuntu bash
List containers
ps shows running; -a includes stopped containers.
docker ps / docker ps -adocker ps -a
Stop / start
Reference containers by name or ID.
docker stop|start iddocker stop my_container
Exec into a container
Run a command inside a running container.
docker exec -it id shdocker exec -it web bash
View logs
-f follows (streams) the log output.
docker logs iddocker logs -f web

Dockerfile

ConceptSyntaxExample
Base image
Every Dockerfile starts from a base image.
FROM image:tagFROM node:20-alpine
Set working dir
Sets the directory for following commands.
WORKDIR /pathWORKDIR /app
Copy files in
ADD also handles URLs and tar extraction.
COPY src destCOPY . /app
Run build command
Executes during build, creating a new image layer.
RUN commandRUN npm install
Expose & default command
CMD is the default process when the container starts.
EXPOSE / CMDEXPOSE 3000 CMD ["node", "server.js"]
Environment variable
Sets an env var available to the running container.
ENV KEY=valueENV NODE_ENV=production

docker compose

ConceptSyntaxExample
Define services
Each service is a container in a multi-container app.
services: in compose.yamlservices: web: image: nginx
Start everything
-d runs in the background; builds images if needed.
docker compose updocker compose up -d
Stop everything
Stops and removes containers, networks created by up.
docker compose downdocker compose down
Build & rebuild
--build forces a rebuild of service images.
docker compose builddocker compose up --build
View service logs
Aggregates logs from one or all services.
docker compose logsdocker compose logs -f web

Volumes & Networks

ConceptSyntaxExample
Bind mount
Maps a host directory into the container for live files.
-v host:containerdocker run -v $(pwd):/app node
Named volume
Docker-managed storage that persists across containers.
-v name:/pathdocker run -v db_data:/var/lib/mysql mysql
List volumes
docker volume create name makes one explicitly.
docker volume lsdocker volume ls
Create a network
Lets containers talk to each other by name.
docker network create namedocker network create app_net
Connect to a network
Attach a container to an existing network.
--network namedocker run --network app_net nginx

Cleanup

ConceptSyntaxExample
Remove a container
Container must be stopped first (or use -f).
docker rm iddocker rm my_container
Remove an image
Fails if a container still uses it.
docker rmi imagedocker rmi myapp:1.0
Prune unused
Reclaims space; -a also removes unused images.
docker system prunedocker system prune -a
Remove all stopped
Deletes every stopped container at once.
docker container prunedocker container prune
Auto-remove on exit
Cleans up the container automatically when it stops.
docker run --rmdocker run --rm alpine echo hi