Docker Commands: Cheat Sheet with Examples

Docker commands cheat sheet: build, run and stop containers, ports and volumes, logs and exec, networks, Docker Compose and cleaning up disk space.

Docker commands grouped by job, each with a short explanation: pulling and building images, running containers with ports, environment variables and volumes, reading logs and opening a shell inside a container, networks, Docker Compose, and cleaning up the disk space Docker quietly fills. Parts in angle brackets, such as <image> or <container>, are what you replace; a container can be named by its name or the first few characters of its ID. Compose is shown as docker compose with a space, the current plugin, and the older docker-compose command takes the same options. The clean-up table is in order from safest to most thorough, and the last one deletes volumes, which hold databases and uploads. The kubectl Commands cheat sheet covers the next step, running containers on Kubernetes, and the Linux Command Cheatsheet covers the shell inside most images.

Images

CommandWhat it does
docker pull <image>Download an image, such as nginx or node:22
docker imagesList images on this machine (also docker image ls)
docker build -t <name>:<tag> .Build an image from the Dockerfile in this folder
docker build -f Dockerfile.dev -t <name> .Build from a different Dockerfile
docker tag <image> <user>/<image>:<tag>Give an image another name, ready to push
docker loginSign in to a registry (Docker Hub unless you name another)
docker push <user>/<image>:<tag>Upload an image to a registry
docker rmi <image>Delete an image
docker image pruneDelete dangling images (untagged leftovers from builds)
docker history <image>Show the layers an image is made of and their sizes

Running containers

CommandWhat it does
docker run <image>Start a container from an image
docker run -d --name web -p 8080:80 nginxRun in the background, named web, with port 8080 on this machine sent to 80 inside
docker run -it ubuntu bashStart a container with an interactive shell
docker run --rm <image>Delete the container when it stops
docker run -e KEY=value <image>Set an environment variable
docker run --env-file .env <image>Set environment variables from a file
docker run -v "$(pwd)":/app <image>Mount the current folder inside the container
docker run -v data:/var/lib/data <image>Use a named volume that outlives the container
docker run --restart unless-stopped <image>Start the container again after crashes and reboots
docker psList running containers
docker ps -aList all containers, stopped ones too
docker stop <container>Stop a container (start and restart work the same way)
docker rm <container>Delete a stopped container (-f stops it first)
docker container pruneDelete all stopped containers

Look inside and debug

CommandWhat it does
docker logs <container>Show a container's output
docker logs -f --tail 100 <container>Follow the output, starting from the last 100 lines
docker exec -it <container> shOpen a shell in a running container (bash if the image has it)
docker inspect <container>Full details: settings, mounts, network, IP address
docker statsLive CPU, memory and network use per container
docker top <container>Processes running inside a container
docker cp <container>:/path ./localCopy a file out of a container (or the other way round)
docker port <container>Which ports are published

Volumes and networks

CommandWhat it does
docker volume lsList volumes
docker volume create <name>Make a volume
docker volume rm <name>Delete a volume and its data
docker network lsList networks
docker network create <name>Make a network: containers on it can reach each other by name
docker run --network <name> <image>Start a container on a network
docker network connect <network> <container>Add a running container to a network

Docker Compose

CommandWhat it does
docker compose upStart everything in compose.yaml (or docker-compose.yml)
docker compose up -dStart in the background
docker compose up --buildRebuild images first
docker compose downStop and remove the containers and networks
docker compose down -vAlso delete the volumes (their data is lost)
docker compose psList the project's containers
docker compose logs -f <service>Follow one service's output
docker compose exec <service> shOpen a shell in a running service
docker compose pullDownload newer images
docker compose restart <service>Restart one service
docker compose configShow the final configuration after variables are filled in

Compose v2 is a plugin, so it's docker compose with a space. The older docker-compose command works the same way.

Clean up

CommandWhat it does
docker system dfHow much space images, containers and volumes use
docker system pruneDelete stopped containers, unused networks, dangling images and build cache
docker image prune -aDelete every image no container uses
docker system prune -a --volumesDelete everything unused, volumes included (careful: data in them is lost)

Common use cases

  • Remember the options for docker run (ports, volumes, environment)
  • Debug a container that keeps stopping
  • Free up disk space Docker is using
  • Learn Docker and Compose for a course or a new job

How to use

  1. Find the table for what you're doing, such as Running containers or Clean up.
  2. Type a word in the filter, such as "logs" or "volume".
  3. Copy the command and replace the parts in angle brackets.
  4. Run docker ps afterwards to check what's running.

FAQ

What's the difference between an image and a container?

An image is the read-only package: the files, libraries and settings an app needs. A container is a running (or stopped) copy of an image with its own writable layer. One image can run as many containers.

Why does my container stop straight away?

A container lives only as long as its main process. If that process finishes or crashes, the container stops. docker logs <container> shows the output, and docker ps -a shows the exit code.

What does -p 8080:80 mean?

The first number is the port on your machine and the second is the port inside the container. With -p 8080:80, opening localhost:8080 reaches the web server listening on port 80 inside.

How do I keep data when a container is deleted?

Store it in a volume (-v name:/path) or a folder on your machine (-v /host/path:/path). Anything written only inside the container's own layer is lost when the container is removed.

Is docker system prune safe?

It removes stopped containers, unused networks, dangling images and build cache, but not volumes, so data is kept. Adding -a removes every image no container uses, and --volumes also deletes unused volumes and the data in them.