Skip to main content

Docker Cheatsheet

Searchable reference of 84 essential Docker commands covering containers, images, volumes, networks, compose, and debugging.

84 commands found

Container Lifecycle

Create and start a container

docker run -d -p 8080:80 --name web nginx:alpine

Create a container without starting

docker create --name myapp node:20-alpine

Start a stopped container

docker start myapp

Gracefully stop a running container

docker stop myapp

Restart a container

docker restart myapp

Force stop a container (SIGKILL)

docker kill myapp

Remove a stopped container

docker rm myapp

Force remove a running container

docker rm -f myapp

Pause all processes in a container

docker pause myapp

Unpause a paused container

docker unpause myapp

Rename a container

docker rename old_name new_name

Block until container stops, print exit code

docker wait myapp

List running containers

docker ps --format 'table {{.Names}}\t{{.Status}}'

List all containers (including stopped)

docker ps -a --filter status=exited

Run and auto-remove on exit

docker run --rm -it alpine sh

Images

Build image from Dockerfile

docker build -t myapp:v1.0 .

Build without using cache

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

List local images

docker images --format '{{.Repository}}:{{.Tag}} {{.Size}}'

Remove an image

docker rmi myapp:v1.0

Tag an image

docker tag myapp:latest registry.io/myapp:v1.0

Pull image from registry

docker pull nginx:alpine

Push image to registry

docker push registry.io/myapp:v1.0

Save image to tar archive

docker save -o myapp.tar myapp:latest

Load image from tar archive

docker load -i myapp.tar

Show image layer history

docker history --no-trunc myapp:latest

Show detailed image/container info

docker inspect --format '{{.Config.Env}}' myapp

Remove unused images

docker image prune -a --filter 'until=24h'

Volumes

Create a named volume

docker volume create pgdata

List all volumes

docker volume ls --filter dangling=true

Show volume details

docker volume inspect pgdata

Remove a volume

docker volume rm pgdata

Remove all unused volumes

docker volume prune -f

Mount a volume to container

docker run -v pgdata:/var/lib/postgresql/data postgres:16

Mount with explicit options

docker run --mount type=bind,src=./app,dst=/app node:20

Copy files between container and host

docker cp myapp:/app/logs ./logs

Networks

Create a network

docker network create --driver bridge mynet

List networks

docker network ls

Show network details

docker network inspect bridge

Connect container to network

docker network connect mynet myapp

Disconnect container from network

docker network disconnect mynet myapp

Remove a network

docker network rm mynet

Remove unused networks

docker network prune -f

Run container in specific network

docker run --network mynet --name api myapp:latest

Compose

Start all services

docker compose up -d --build

Stop and remove containers

docker compose down --volumes --remove-orphans

List compose services

docker compose ps --format json

View service logs

docker compose logs -f --tail=100 api

Build or rebuild services

docker compose build --no-cache api

Pull service images

docker compose pull

Execute command in running service

docker compose exec db psql -U postgres

Run one-off command

docker compose run --rm api npm test

Restart services

docker compose restart api worker

Validate and view config

docker compose config --services

Scale a service

docker compose up -d --scale worker=3

System/Cleanup

Show Docker disk usage

docker system df -v

Remove all unused data

docker system prune -a --volumes -f

Remove all stopped containers

docker container prune -f

Remove all unused images

docker image prune -a -f

Remove build cache

docker builder prune --all -f

Display system-wide information

docker system info --format '{{.ServerVersion}}'

Get real-time events

docker system events --filter type=container

Live resource usage statistics

docker stats --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}'

Show total disk usage breakdown

docker system df --format '{{.Type}}: {{.Size}}'

Manage Docker contexts

docker context ls

Debugging

View container logs

docker logs -f --tail=50 myapp

Run command in running container

docker exec -it myapp /bin/sh

Attach to running container

docker attach myapp

Display running processes

docker top myapp

List port mappings

docker port myapp

Show filesystem changes

docker diff myapp

Show container details

docker inspect --format '{{.State.Status}}' myapp

View logs since timestamp

docker logs --since 2h myapp

Monitor Docker events

docker events --filter container=myapp

Check environment variables

docker exec myapp env | sort

Check listening ports inside container

docker exec myapp netstat -tlnp

Check container IP address

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp

Registry

Log in to a registry

docker login registry.io -u username

Log out from a registry

docker logout registry.io

Search Docker Hub

docker search --filter is-official=true nginx

Inspect image manifest

docker manifest inspect nginx:alpine

Build multi-platform image

docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .

List builder instances

docker buildx ls

Tag and push to ECR/GCR/ACR

docker tag app:v1 123456.dkr.ecr.us-east-1.amazonaws.com/app:v1

Pull specific platform image

docker pull --platform linux/arm64 nginx:alpine

What is Docker?

Docker is a platform for building, shipping, and running applications in containers. Containers package an application with all its dependencies into a standardized unit, ensuring it runs identically across development, staging, and production environments. Docker has become the industry standard for containerization and is fundamental to modern DevOps workflows, CI/CD pipelines, and microservices architectures.

This Docker cheatsheet covers the most commonly used commands organized by workflow: managing container lifecycles, building and managing images, working with volumes and networks, orchestrating with Docker Compose, cleaning up resources, debugging issues, and interacting with registries. Whether you are a beginner learning Docker or an experienced DevOps engineer looking for quick reference, these commands cover the full spectrum of daily Docker operations.

Frequently Asked Questions

How to remove all Docker images?

Use docker image prune -a -f to remove all images not associated with a running container. To remove absolutely everything including volumes and networks, use docker system prune -a --volumes -f. Be careful in production—this removes all cached layers and will require re-pulling images. To remove specific images, use docker rmi image:tag or filter with docker images --filter dangling=true -q | xargs docker rmi.

How to enter a running container?

Use docker exec -it container_name /bin/sh (or /bin/bash if available) to get an interactive shell inside a running container. The -i flag keeps STDIN open and -t allocates a pseudo-TTY. For distroless or minimal images without a shell, use docker debug (Docker Desktop) or attach an ephemeral debug container with kubectl debug in Kubernetes environments.

How to check Docker disk usage?

Use docker system df to see disk usage broken down by images, containers, volumes, and build cache. Add -v for detailed per-item breakdown. Common culprits for high disk usage are dangling images (intermediate build layers), stopped containers with large logs, unused volumes, and build cache. Use docker system prune to reclaim space, or target specific resources with docker image prune, docker container prune, or docker volume prune.