Docker Cheat Sheet: Command Reference by Purpose
When working with Docker day-to-day, it's easy to forget exact options and syntax. This guide organizes the most commonly used Docker commands by what you're trying to do, so you can find what you need quickly.
docker
Description
Images
List all images stored locally.
docker images
Pull an image from a registry such as Docker Hub.
docker pull nginx:latest
Build an image from a Dockerfile. Use -t to specify the image name and tag.
docker build -t my-app:latest .
Starting Containers
Use docker run to create and start a new container from an image. Use -d for detached (background) mode and --name to assign a name.
docker run -d --name my-container nginx
Map ports with the -p option in <host-port>:<container-port> format.
docker run -d -p 8080:80 --name my-nginx nginx
Start a stopped container.
docker start <container-name-or-id>
Restart a running container — useful when you want to apply updated configuration.
docker restart <container-name-or-id>
Container Status
List running containers. Add -a to include stopped containers as well.
docker ps
docker ps -a
Show detailed information about a specific container (IP address, mounts, environment variables, etc.).
docker inspect <container-name-or-id>
Monitor real-time resource usage (CPU and memory) for containers.
docker stats
Show overall disk usage (images, containers, volumes, and build cache). Useful before cleaning up to understand how much space is in use.
docker system df
View container logs. Use -f to follow logs in real time, and --tail to limit the number of lines shown.
docker logs <container-name-or-id>
docker logs -f <container-name-or-id>
docker logs --tail 100 <container-name-or-id>
List volumes.
docker volume ls
Show detailed information about a volume (including its mount path).
docker volume inspect <volume-name>
List networks.
docker network ls
Show details about a network, including connected containers and configuration.
docker network inspect <network-name>
Container Operations
Open a shell inside a running container. Use sh if the container is Alpine-based and doesn't have bash.
docker exec -it <container-name-or-id> bash
docker exec -it <container-name-or-id> sh
Run a one-off command inside a container without opening an interactive shell.
docker exec <container-name-or-id> ls /etc
Copy a file from the host into a container — handy for pushing configuration files.
docker cp <host-path> <container-name-or-id>:<container-path>
Copy a file from a container to the host — useful for retrieving logs or configuration files.
docker cp <container-name-or-id>:<container-path> <host-path>
Stopping and Removing
Stop a running container. Docker sends SIGTERM, and falls back to SIGKILL if the container doesn't stop within the timeout.
docker stop <container-name-or-id>
Remove a stopped container. Add -f to force-remove a running container.
docker rm <container-name-or-id>
docker rm -f <container-name-or-id>
Use prune to bulk-remove stopped containers, unused images, or unused volumes.
docker container prune
docker image prune
docker volume prune
Remove all stopped containers, unused images, unused networks, and build cache in one go. Add --volumes to also remove unused volumes.
docker system prune
docker system prune --volumes
Summary
- The basic flow to get a container running is: pull an image → start the container → check its status
- When something goes wrong, check the logs first, then exec into the container to investigate
- Make it a habit to check disk usage regularly and clean up unused resources