Docker packages and runs applications in containers. This cheat sheet covers the commands used most often — from basic container management to Docker Compose and system cleanup.
1. Container Management
Command
What it does
docker ps
List running containers
docker ps -a
List all containers including stopped
docker run -d --name my-app -p 8080:80 <image>
Run detached with name and port mapping
docker run -it <image> bash
Run with interactive terminal
docker run --rm <image>
Auto-remove container when it exits
docker run -e KEY=value <image>
Set environment variable
docker run -v /host/path:/container/path <image>
Bind-mount a host directory into the container
docker start / stop / restart <container>
Start, stop, or restart a container
docker kill <container>
Immediately force-stop (SIGKILL)
docker rm <container>
Remove a stopped container
docker rm -f <container>
Force-remove a running container
docker exec -it <container> bash
Open shell inside running container
docker inspect <container>
Show detailed container info (JSON)
docker cp <container>:/path ./local
Copy file from container to host
docker stats
Live CPU/memory/network usage
Tip: Stop all running containers at once: docker stop $(docker ps -q)
2. Image Commands
Command
What it does
docker images
List local images
docker pull <image>:<tag>
Download image from registry
docker rmi <image>
Remove a local image
docker tag <image> <new-name>:<tag>
Tag an image with a new name
docker history <image>
Show image layer history
docker save -o image.tar <image>
Export image to tar file
docker load -i image.tar
Load image from tar file
3. Volumes
Command
What it does
docker volume ls
List all volumes
docker volume create <name>
Create a named volume
docker volume inspect <name>
Show volume details
docker volume rm <name>
Remove a volume
docker volume prune
Remove unused anonymous volumes (-a for named ones too)
docker run -v my-vol:/app/data <image>
Mount named volume into container
4. Networks
Command
What it does
docker network ls
List all networks
docker network create <name>
Create a custom bridge network
docker network connect <network> <container>
Connect container to a network
docker network disconnect <network> <container>
Disconnect container from network
docker network prune
Remove all unused networks
docker run --network=host <image>
Connect the container to the host network
5. Docker Compose
Docker Compose manages multi-container applications defined in a Compose file such as compose.yaml.
Command
What it does
docker compose up -d
Start all services in background
docker compose up --build
Rebuild images before starting
docker compose down
Stop and remove containers and networks
docker compose down -v
Also remove volumes
docker compose ps
List service containers
docker compose logs -f <service>
Follow logs for a specific service
docker compose exec <service> bash
Open shell in a running service
docker compose restart <service>
Restart a specific service
docker compose pull
Pull the images for all services
docker compose run <service> <cmd>
Run a one-off command in a service
docker compose config
Validate and view merged compose config
6. Logs & Monitoring
Command
What it does
docker logs -f <container>
Follow log output in real time
docker logs --tail 100 <container>
Show last 100 log lines
docker logs --since 1h <container>
Show logs from last hour
docker stats <container>
Resource usage for specific container
docker events
Stream real-time Docker daemon events
7. Building Images
Command
What it does
docker build -t myapp:1.0 .
Build and tag from current directory
docker build -f Dockerfile.prod .
Build using a specific Dockerfile
docker build --no-cache .
Build without cache (fresh build)
docker build --build-arg KEY=val .
Pass build-time variable
docker build --target <stage> .
Build up to a specific multi-stage target
Multi-stage builds: with --target the named stage becomes the final stage of the image and the builder skips the stages after it, which lets you build only one stage during development.
Widest cleanup: also unused anonymous volumes (named volumes are kept)
docker container prune
Remove all stopped containers
docker image prune -a
Remove all unused images
docker builder prune
Clear dangling build cache (-a for all unused cache)
Warning:docker system prune -a --volumes removes every unused image and every unused anonymous volume. Named volumes are kept; docker volume prune -a is the command that deletes those. Back up data before running either in production.
10. Power Tips & One-liners
Command
What it does
docker stop $(docker ps -q)
Stop all running containers
docker rm $(docker ps --filter status=exited -q)
Remove all exited containers
docker rmi $(docker images -q)
Remove all local images (one used by a running container needs -f)
The commands are grouped by what they act on — containers, images, volumes, networks, Compose — which matches how Docker's own CLI is organised (docker container, docker image). The short forms most people type (docker ps, docker rm) are aliases of those, and the sheet uses the long form only where no short one exists (docker container prune, docker image prune). Placeholders in angle brackets are values to replace; names such as my-app, my-vol and myapp:1.0 are examples.
Three habits the sheet cannot teach
First, name containers and networks; --name on run turns every later command from an ID lookup into a word. Second, keep state in named volumes, not in the container, so docker rm is never a data-loss event. Third, read docker system df before pruning, because the cleanup commands delete exactly what they say. The Docker reference page on this site has a row for each of these — docker run --name, mounting a named volume, docker system df and the prune commands — with a one-line note on what each does.
Compose versus plain docker
Anything with more than one container belongs in a compose.yaml, and the Compose section of the sheet replaces most of the run-and-network commands above it: docker compose up -d creates the network, volumes and containers from the file, and down removes the containers and network again; the volumes stay unless you add -v. The plain commands remain the tools for inspecting what Compose created.