Docker Cheat Sheet — Commands & Compose

The Docker and Docker Compose commands you reach for every week, explained in one line. Set your image, container, ports and service once — every command updates and is ready to copy.

Runs in your browserUpdated
Your valuesEvery example below updates as you type

Images

10
docker pull node:22-alpine

Download an image from a registry.

docker image ls

List local images with their sizes.

docker build -t myapp:1.0 .

Build an image from the Dockerfile in the current folder.

docker build -t myapp:1.0 -f Dockerfile.prod .

Build with a different Dockerfile.

docker build --no-cache -t myapp:1.0 .

Rebuild every layer from scratch.

docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/acme/myapp:1.0 --push .

Build for Intel and ARM (e.g. Apple Silicon, Graviton) and push both.

docker tag myapp:1.0 ghcr.io/acme/myapp:1.0

Give an image a registry name before pushing.

docker push ghcr.io/acme/myapp:1.0

Upload an image to a registry.

docker history myapp:1.0

Show the layers of an image and how big each one is.

docker rmi myapp:1.0

destructive Delete a local image.

Run containers

8
docker run -d --name web -p 8080:3000 myapp:1.0

Run in the background and publish container port {{cport}} on host port {{hport}}.

docker run --rm -it node:22-alpine sh

Start a throwaway interactive shell; the container is removed when you exit.

docker run -d --restart unless-stopped --name web myapp:1.0

Restart automatically after crashes and reboots.

docker run --env-file .env -e NODE_ENV=production myapp:1.0

Pass environment variables from a file and on the command line.

docker run -v app-data:/data myapp:1.0

Mount a named volume so data survives container removal.

docker run --rm -v "$(pwd)":/app -w /app node:22-alpine npm test

Mount the current folder and run a command inside it.

docker run --memory 512m --cpus 1.5 myapp:1.0

Limit memory and CPU.

docker run -d --network app-net --name web myapp:1.0

Attach to a user-defined network so containers reach each other by name.

Manage containers

10
docker ps -a

List all containers, including stopped ones.

docker logs -f --tail 100 web

Follow the last 100 log lines.

docker exec -it web sh

Open a shell in a running container (use bash if the image has it).

docker stop web

Stop gracefully (SIGTERM, then SIGKILL after 10 s).

docker restart web

Restart a container.

docker cp web:/app/logs ./logs

Copy files out of a container (swap the arguments to copy in).

docker stats

Live CPU, memory, network and disk usage of running containers.

docker inspect -f '{{.State.Health.Status}}' web

Read one field from the container details — here the health-check status.

docker port web

Show which host ports map to the container.

docker rm -f web

destructive Stop and delete a container.

Docker Compose

12
docker compose up -d

Start all services in the background.

docker compose up -d --build

Rebuild images, then start.

docker compose ps

List the services and their state.

docker compose logs -f api

Follow the logs of one service.

docker compose exec api sh

Open a shell in a running service.

docker compose run --rm api npm run migrate

Run a one-off command in a new container for a service.

docker compose restart api

Restart one service.

docker compose pull

Pull the latest images for all services.

docker compose config

Validate the file and print the final merged configuration.

docker compose -f compose.yml -f compose.prod.yml up -d

Layer an override file on top of the base file.

docker compose down

Stop and remove containers and networks (volumes are kept).

docker compose down -v

destructive Also delete the volumes — your databases are gone.

Volumes & networks

6
docker volume ls

List volumes.

docker volume inspect app-data

Show where a volume lives on the host.

docker run --rm -v app-data:/data -v "$(pwd)":/backup alpine tar czf /backup/app-data.tar.gz -C /data .

Back up a volume to a .tar.gz in the current folder.

docker network create app-net

Create a network for containers that need to talk to each other.

docker network connect app-net web

Attach a running container to a network.

docker volume rm app-data

destructive Delete a volume and its data.

Clean up disk space

5
docker system df

Show how much space images, containers, volumes and build cache use.

docker builder prune

Remove unused build cache.

docker image prune -a

destructive Remove all images not used by a container.

docker system prune

destructive Remove stopped containers, unused networks, dangling images and build cache.

docker system prune -a --volumes

destructive Remove everything unused, including volumes. Last resort.

Debugging

5
docker run --rm -it --entrypoint sh myapp:1.0

Start an image with a shell instead of its normal command — for images that crash on start.

docker logs web 2>&1 | grep -i error

Search a container’s logs (stdout and stderr).

docker exec web env

Print the environment variables a container actually sees.

docker compose exec api wget -qO- localhost:3000/health

Call a health endpoint from inside the service.

docker events --since 10m

Stream recent Docker events: starts, stops, OOM kills, health changes.

Your data never leaves your computer. All processing happens 100% locally in your browser.

How to use the Docker Cheat Sheet

  1. Type your own values (image, container, ports, service) in the boxes at the top — every example on the page updates instantly.
  2. Search or jump to a section to find the command you need.
  3. Click Copy to copy a ready-to-run line, or Copy section for a commented block.
  4. Commands marked destructive delete or overwrite data — read the note before running them.

Images, containers, volumes and networks

An image is a packaged filesystem and start command, built from a Dockerfile. A container is a running copy of an image. Anything a container writes disappears when it is removed, unless it goes into a volume. Containers on the same user-defined network reach each other by name, which is how Compose services talk to their database.

Reading -p and -v

Both flags are host:container. -p 8080:3000 means “requests to port 8080 on my machine go to port 3000 in the container”. -v app-data:/data mounts the named volume app-data at /data; -v "$(pwd)":/app mounts your current folder instead, which is how you get live code reloading in development.

Keeping images small and builds fast

  • Start from slim bases such as -alpine or -slim, and use multi-stage builds so compilers don’t ship to production.
  • Copy dependency files and install before copying the rest of the code, so the install layer stays cached.
  • Add a .dockerignore for node_modules, .git and build output.

Write Compose files with the Docker Compose generator, and set up CI with the GitHub Actions generator.

Tools that pair well with the Docker Cheat Sheet

  • Linux Commands Cheat Sheet — The Linux and bash commands developers use on servers and laptops — files, search, permissions, processes, logs, networking and SSH.
  • Git Cheat Sheet — Every Git command you use day to day — and the ones you need when something goes wrong — explained in one line.
  • Nginx Config Generator — Choose what Nginx should do — serve a static site or SPA, proxy to a Node/Python app, or run PHP — and tick the extras.
  • AI Prompts for Developers — Battle-tested prompts for the coding tasks you do every day — debugging, reviews, tests, refactoring, SQL, commit messages and documentation.

Frequently asked questions

What is the difference between an image and a container?
An image is a read-only template built from a Dockerfile. A container is a running (or stopped) instance of an image with its own writable layer. One image can run as many containers.
How do I free up disk space used by Docker?
Check usage with docker system df, then remove build cache with docker builder prune and unused images with docker image prune -a. docker system prune removes more at once — read what it deletes first.
Is it docker compose or docker-compose?
Use docker compose (with a space). It is Compose v2, built into the Docker CLI. The separate docker-compose v1 tool is deprecated.
Why can’t my containers reach each other?
Containers on the default bridge network can’t resolve each other by name. Put them on a user-defined network (docker network create) or the same Compose project, then use the service or container name as the hostname.

Related tools