Docker Cheat Sheet
Images, containers, Compose, and cleanup at a glance.
The Docker commands that come up when you build, run, and debug containers — plus the Dockerfile and Compose instructions worth memorizing. Skip the exhaustive flag list; this is the working set.
If your machine is filling up, jump to the cleanup section: `docker system prune` reclaims most of the space, and knowing what it removes keeps you from deleting something you needed.
Images
docker build -t name:tag . | Build an image from the Dockerfile in `.`. |
docker images | List local images. |
docker pull name:tag | Download an image from a registry. |
docker push name:tag | Upload an image to a registry. |
docker rmi name:tag | Remove an image. |
docker tag src name:tag | Add a new tag to an image. |
Containers
docker run -d -p 8080:80 name | Run detached, mapping host:container ports. |
docker run --rm -it name sh | Run interactively; auto-remove on exit. |
docker ps · docker ps -a | List running, or all, containers. |
docker logs -f <id> | Stream a container’s logs. |
docker exec -it <id> sh | Open a shell inside a running container. |
docker stop <id> · docker rm <id> | Stop, then remove a container. |
Dockerfile essentials
FROM node:20-alpine | Base image the build starts from. |
WORKDIR /app | Set the working directory for later steps. |
COPY package*.json ./ | Copy deps first so the layer caches. |
RUN npm ci | Run a build step in a new layer. |
EXPOSE 3000 | Document the port the app listens on. |
CMD ["node", "server.js"] | Default command when the container starts. |
Volumes & networks
docker volume create data | Create a named volume for persistence. |
docker run -v data:/var/lib/x name | Mount a named volume into a container. |
docker run -v $PWD:/app name | Bind-mount the current directory. |
docker network create net | Create a user-defined bridge network. |
docker network connect net <id> | Attach a container to a network. |
Compose
docker compose up -d | Start all services in the background. |
docker compose down | Stop and remove services (add -v for volumes). |
docker compose logs -f svc | Follow logs for one service. |
docker compose ps | List the project’s running services. |
docker compose build | Rebuild service images. |
Cleanup
Reclaims disk — read what each removes before running it.
docker system prune | Remove stopped containers, unused networks, dangling images. |
docker system prune -a --volumes | Also remove unused images and volumes. |
docker image prune | Remove dangling images only. |
docker container prune | Remove all stopped containers. |
Frequently asked questions
What is the difference between a Docker image and a container?
An image is a read-only template — the built filesystem plus metadata. A container is a running (or stopped) instance of an image with its own writable layer. One image can spawn many containers.
How do I free up disk space used by Docker?
Run `docker system prune` to remove stopped containers, unused networks, and dangling images. Add `-a --volumes` to also drop unused images and volumes — but review what will be deleted first, since volumes may hold data you want to keep.