Docker Cheatsheet

Docker commands for containers, images, Compose, diagnostics, and cleanup.

Reference

On this page

Containers #

# List all running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Start a container
docker start <container_name>

# Stop a container
docker stop <container_name>

# Restart a container
docker restart <container_name>

# Remove a container
docker rm <container_name>

Images #

# List images
docker images

# Pull an image
docker pull ubuntu:latest

# Remove an image
docker rmi ubuntu:latest

# Build an image from a Dockerfile
docker build -t myimage:latest .

Inspect and troubleshoot #

# Show logs for a container
docker logs <container_name>

# Tail logs (live updates)
docker logs -f <container_name>

# Execute a command inside a running container
docker exec -it <container_name> bash

# Inspect container details (network, volumes, etc)
docker inspect <container_name>

Compose #

# Start services in the background
docker compose up -d

# Pull latest images for all services
docker compose pull

# Stop and remove service containers and their networks
docker compose down

# Rebuild and start
docker compose up --build

# View logs from all services
docker compose logs -f

Cleanup #

# Remove stopped containers
docker container prune

# Remove dangling images
docker image prune

# Remove stopped containers, unused networks/images, and build cache (not volumes)
docker system prune -a

One-off tasks #

# Run a one-off container
docker run --rm -it alpine sh

# Copy files from container to host
docker cp <container_name>:/path/in/container /path/on/host

Pruning removes data. Inspect what is in use before cleanup; volumes are not removed by docker system prune -a. See the Docker prune reference.