Docker Cheat Sheet
Free Docker cheat sheet: the everyday docker and docker compose commands, Dockerfile instructions and image/container basics at a glance.
Images
| Concept | Syntax | Example |
|---|---|---|
| Pull an image Downloads an image from a registry like Docker Hub. | docker pull image:tag | docker 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 images | docker images |
| Tag an image Add a new name/tag, e.g. before pushing to a registry. | docker tag src target | docker tag myapp:1.0 user/myapp:latest |
| Push an image Uploads to a registry; log in first with docker login. | docker push name:tag | docker push user/myapp:latest |
Containers
| Concept | Syntax | Example |
|---|---|---|
| Run a container -d detaches (background); -p maps host:container ports. | docker run image | docker run -d -p 8080:80 nginx |
| Run interactively -it gives an interactive terminal inside the container. | docker run -it image sh | docker run -it ubuntu bash |
| List containers ps shows running; -a includes stopped containers. | docker ps / docker ps -a | docker ps -a |
| Stop / start Reference containers by name or ID. | docker stop|start id | docker stop my_container |
| Exec into a container Run a command inside a running container. | docker exec -it id sh | docker exec -it web bash |
| View logs -f follows (streams) the log output. | docker logs id | docker logs -f web |
Dockerfile
| Concept | Syntax | Example |
|---|---|---|
| Base image Every Dockerfile starts from a base image. | FROM image:tag | FROM node:20-alpine |
| Set working dir Sets the directory for following commands. | WORKDIR /path | WORKDIR /app |
| Copy files in ADD also handles URLs and tar extraction. | COPY src dest | COPY . /app |
| Run build command Executes during build, creating a new image layer. | RUN command | RUN npm install |
| Expose & default command CMD is the default process when the container starts. | EXPOSE / CMD | EXPOSE 3000
CMD ["node", "server.js"] |
| Environment variable Sets an env var available to the running container. | ENV KEY=value | ENV NODE_ENV=production |
docker compose
| Concept | Syntax | Example |
|---|---|---|
| Define services Each service is a container in a multi-container app. | services: in compose.yaml | services:
web:
image: nginx |
| Start everything -d runs in the background; builds images if needed. | docker compose up | docker compose up -d |
| Stop everything Stops and removes containers, networks created by up. | docker compose down | docker compose down |
| Build & rebuild --build forces a rebuild of service images. | docker compose build | docker compose up --build |
| View service logs Aggregates logs from one or all services. | docker compose logs | docker compose logs -f web |
Volumes & Networks
| Concept | Syntax | Example |
|---|---|---|
| Bind mount Maps a host directory into the container for live files. | -v host:container | docker run -v $(pwd):/app node |
| Named volume Docker-managed storage that persists across containers. | -v name:/path | docker run -v db_data:/var/lib/mysql mysql |
| List volumes docker volume create name makes one explicitly. | docker volume ls | docker volume ls |
| Create a network Lets containers talk to each other by name. | docker network create name | docker network create app_net |
| Connect to a network Attach a container to an existing network. | --network name | docker run --network app_net nginx |
Cleanup
| Concept | Syntax | Example |
|---|---|---|
| Remove a container Container must be stopped first (or use -f). | docker rm id | docker rm my_container |
| Remove an image Fails if a container still uses it. | docker rmi image | docker rmi myapp:1.0 |
| Prune unused Reclaims space; -a also removes unused images. | docker system prune | docker system prune -a |
| Remove all stopped Deletes every stopped container at once. | docker container prune | docker container prune |
| Auto-remove on exit Cleans up the container automatically when it stops. | docker run --rm | docker run --rm alpine echo hi |