Docker & Docker Compose Cheat Sheet

Essential Docker commands for container management, images, networking, volumes, and debugging.

Container Lifecycle

Run a container from an image docker run <image>
Run in detached mode with port mapping docker run -d -p 8080:80 <image>
Run interactively with a shell docker run -it <image> /bin/bash
Run with environment variables docker run -e KEY=value <image>
Run with a name docker run --name my-app <image>
Start a stopped container docker start <container>
Stop a running container docker stop <container>
Restart a container docker restart <container>
Remove a stopped container docker rm <container>
Force remove a running container docker rm -f <container>
Execute a command in a running container docker exec -it <container> /bin/bash
View container logs docker logs <container>
Follow container logs in real time docker logs -f <container>
Inspect container details docker inspect <container>
List running containers docker ps
List all containers (including stopped) docker ps -a

Images

Build an image from a Dockerfile docker build -t <name>:<tag> .
Build with a specific Dockerfile docker build -f Dockerfile.prod -t <name> .
Pull an image from a registry docker pull <image>:<tag>
Push an image to a registry docker push <image>:<tag>
Tag an image docker tag <image> <repo>:<tag>
Remove an image docker rmi <image>
List all local images docker images
Remove all unused images docker image prune -a
Show image build history docker history <image>

Docker Compose

Start all services in the background docker compose up -d
Start with build docker compose up -d --build
Stop and remove all services docker compose down
Stop and remove including volumes docker compose down -v
Build or rebuild services docker compose build
View logs of all services docker compose logs
Follow logs of a specific service docker compose logs -f <service>
List running services docker compose ps
Execute a command in a service docker compose exec <service> <cmd>
Run a one-off command in a service docker compose run <service> <cmd>
Pull latest images for services docker compose pull
Restart services docker compose restart

Networking

Create a network docker network create <name>
List all networks docker network ls
Remove a network docker network rm <name>
Inspect a network docker network inspect <name>
Connect a container to a network docker network connect <network> <container>
Disconnect a container from a network docker network disconnect <network> <container>
Map host port to container port docker run -p <host>:<container> <image>
Expose all ports to random host ports docker run -P <image>

Volumes

Create a named volume docker volume create <name>
List all volumes docker volume ls
Remove a volume docker volume rm <name>
Inspect a volume docker volume inspect <name>
Remove all unused volumes docker volume prune
Mount a volume to a container docker run -v <volume>:/path <image>
Bind mount a host directory docker run -v /host/path:/container/path <image>
Mount as read-only docker run -v <volume>:/path:ro <image>

Debugging

View container logs with timestamps docker logs -t <container>
View last N lines of logs docker logs --tail 100 <container>
Open a shell in a running container docker exec -it <container> sh
Inspect container details as JSON docker inspect <container>
View real-time resource usage stats docker stats
View running processes in a container docker top <container>
Show port mappings for a container docker port <container>
Copy files from container to host docker cp <container>:/path /host/path
View Docker disk usage docker system df
Remove all unused resources docker system prune -a