Contents
Docker Commands Cheat Sheet: Essential Commands for Containers, Images, and More
This page collects the Docker commands you use most in day-to-day work, based on Docker 29. For detailed explanations and actual output for each command, follow the links in each section. The final section covers common pitfalls found while testing this tutorial.
Containers
| Command | Description |
|---|---|
docker run -d --name web -p 8080:80 nginx:1.30 | Run a container in the background and map a port |
docker run -it --rm ubuntu:26.04 bash | Run interactively and remove the container when it exits |
docker ps | List running containers |
docker ps -a | List all containers |
docker stop web | Stop a container |
docker start web | Start a stopped container |
docker restart web | Restart a container |
docker rm web | Remove a stopped container |
docker rm -f web | Force-remove a running container |
docker logs -f web | Follow a container's logs |
docker logs --tail 100 web | Show the last 100 log lines |
docker exec -it web sh | Open a shell in a container |
docker cp web:/path/file . | Copy a file from a container to the host |
docker inspect web | View detailed container information |
docker stats | View resource usage in real time |
docker top web | Show processes inside a container |
docker port web | View port mappings |
docker update --restart unless-stopped web | Change the restart policy |
docker run --init ... | Use a small init process as PID 1 to forward stop signals correctly |
Further reading: docker run options, Container lifecycle, View logs and debug
Images
| Command | Description |
|---|---|
docker images | List local images |
docker images --tree | Show the platforms included in multi-platform images |
docker pull nginx:1.30 | Pull an image |
docker tag myapp user/myapp:1.0 | Add a new name or tag |
docker push user/myapp:1.0 | Push to a registry |
docker rmi nginx:1.30 | Remove an image |
docker history nginx:1.30 | View image layers |
docker buildx imagetools inspect nginx:1.30-alpine | View the platforms supported by a remote image |
docker save -o img.tar myapp | Export an image to a file |
docker load -i img.tar | Load an image from a file |
docker login / docker logout | Log in to / out of a registry |
Further reading: Images, Push to Docker Hub
Builds
| Command | Description |
|---|---|
docker build -t myapp . | Build with the Dockerfile in the current directory |
docker build -f Dockerfile.prod -t myapp . | Specify a Dockerfile |
docker build --no-cache -t myapp . | Build without using the cache |
docker build --pull -t myapp . | Pull the base image again |
docker build --target dev -t myapp:dev . | Build only up to a specific stage |
docker build --build-arg VERSION=1.2 . | Pass a build argument (do not use it to pass passwords) |
docker build --secret id=token,env=TOKEN . | Pass a build secret without leaving it in the image |
docker build --progress plain . | Show the full build output |
docker buildx build --platform linux/amd64,linux/arm64 -t user/app --push . | Build for multiple platforms and push |
Further reading: Dockerfile instructions, Build cache and optimization, Multi-stage builds, Multi-platform images
Volumes
| Command | Description |
|---|---|
docker volume create data | Create a volume |
docker volume ls | List volumes |
docker volume inspect data | View detailed information |
docker volume rm data | Remove a volume |
docker run -v data:/app/data myapp | Mount a named volume |
docker run -v "$(pwd):/app" myapp | Mount the current directory |
Further reading: Volumes and data persistence
Networks
| Command | Description |
|---|---|
docker network create appnet | Create a custom network |
docker network ls | List networks |
docker network inspect appnet | View the containers on a network |
docker run --network appnet --name db postgres:18 | Connect a container to a network |
docker network connect appnet web | Connect a running container to a network |
docker network rm appnet | Remove a network |
Further reading: Networking and container communication
Docker Compose
| Command | Description |
|---|---|
docker compose up -d | Start all services in the background |
docker compose up -d --build | Rebuild and start |
docker compose up -d --wait | Wait until all services are healthy before returning |
docker compose up -d app | Update or rebuild only one service |
docker compose down | Stop and remove containers and networks |
docker compose down -v | Also remove volumes |
docker compose ps | Show service status |
docker compose logs -f app | Follow a service's logs |
docker compose exec app sh | Open a shell in a service container |
docker compose run --rm app npm test | Run a one-off command |
docker compose pull | Pull the latest images |
docker compose restart app | Restart a service (does not apply changed configuration) |
docker compose watch | Automatically sync or rebuild when files change (for development) |
docker compose --profile tools up -d | Also start services in the profile |
docker compose config | Check the final configuration |
Further reading: Docker Compose introduction, docker compose commands
Cleanup
| Command | Description |
|---|---|
docker system df | View disk usage |
docker container prune | Remove stopped containers |
docker image prune -a | Remove unused images |
docker builder prune | Clear the build cache |
docker builder prune --filter until=168h | Clear only build cache unused for more than 7 days |
docker volume prune | Remove unused anonymous volumes |
docker volume prune -a | Remove all unused volumes, including named database volumes |
docker system prune | General cleanup (does not include volumes) |
docker system prune --volumes | General cleanup plus anonymous volumes (does not include named volumes as of Docker 23) |
Further reading: Free up disk space
System information
| Command | Description |
|---|---|
docker version | Client and server versions |
docker info | Docker system information |
docker events | View events in real time |
docker context ls | List Docker environments you can connect to |
Common options at a glance
| Option | Meaning |
|---|---|
-d | Run in the background |
-it | Interactive mode + terminal |
-p host:container | Port mapping |
-v source:container-path | Mount |
-e KEY=VALUE | Environment variable |
--name | Container name |
--rm | Remove automatically when it exits |
--network | Specify a network |
--restart | Restart policy |
-f | Force (rm, rmi) or follow (logs) |
Common pitfalls (found while testing this tutorial)
| Symptom | Cause and fix | Chapter |
|---|---|---|
docker stop takes 10 seconds | The program is PID 1 and does not handle SIGTERM. Handle it in the program or add --init | Node.js application |
Connecting to 127.0.53.53 when the container is named web | .web is a top-level domain, so the name is resolved as an external domain. Choose another name | docker run options |
postgres:18 exits as soon as it starts | Version 18 requires mounting /var/lib/postgresql, not /var/lib/postgresql/data | PostgreSQL |
| Database reports healthy before it is ready | Add -h 127.0.0.1 to pg_isready and mysqladmin ping | MySQL, PostgreSQL |
| Health check passes even when the Redis password is wrong | redis-cli ping still exits with code 0 on error; add | grep -q PONG | Redis |
Changing MYSQL_ROOT_PASSWORD has no effect | The password variable is used only during the first initialization | MySQL |
| The container still has the old config file after you edit it | When you mount a single file, saving in the editor replaces the file. Restart the container or mount the directory instead | Nginx |
Values from --env-file include quotes | docker run --env-file does not strip quotes; Compose's env_file does | Environment variables |
A secret removed with rm is still in the image | Every layer retains its contents; ARG and ENV are visible too. Use --secret | Security best practices |
exec format error | The CPU architectures differ; use --platform or build for multiple platforms | Multi-platform images |
FAQ
What if I can't remember all the commands?
Start by remembering run, ps, logs, exec, stop, rm, build, and compose up/down. Look up the others here or run docker command --help when you need them.
Where can I find the full options for each command?
See the Command reference chapters for complete options and examples of common commands, such as [docker build](/docker/docker-build/) and [docker ps](/docker/docker-ps/).
Updated October 14, 2026