Skip to content

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 imagesList local images.
docker pull name:tagDownload an image from a registry.
docker push name:tagUpload an image to a registry.
docker rmi name:tagRemove an image.
docker tag src name:tagAdd a new tag to an image.

Containers

docker run -d -p 8080:80 nameRun detached, mapping host:container ports.
docker run --rm -it name shRun interactively; auto-remove on exit.
docker ps · docker ps -aList running, or all, containers.
docker logs -f <id>Stream a container’s logs.
docker exec -it <id> shOpen a shell inside a running container.
docker stop <id> · docker rm <id>Stop, then remove a container.

Dockerfile essentials

FROM node:20-alpineBase image the build starts from.
WORKDIR /appSet the working directory for later steps.
COPY package*.json ./Copy deps first so the layer caches.
RUN npm ciRun a build step in a new layer.
EXPOSE 3000Document the port the app listens on.
CMD ["node", "server.js"]Default command when the container starts.

Volumes & networks

docker volume create dataCreate a named volume for persistence.
docker run -v data:/var/lib/x nameMount a named volume into a container.
docker run -v $PWD:/app nameBind-mount the current directory.
docker network create netCreate a user-defined bridge network.
docker network connect net <id>Attach a container to a network.

Compose

docker compose up -dStart all services in the background.
docker compose downStop and remove services (add -v for volumes).
docker compose logs -f svcFollow logs for one service.
docker compose psList the project’s running services.
docker compose buildRebuild service images.

Cleanup

Reclaims disk — read what each removes before running it.

docker system pruneRemove stopped containers, unused networks, dangling images.
docker system prune -a --volumesAlso remove unused images and volumes.
docker image pruneRemove dangling images only.
docker container pruneRemove 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.