require(reticulate)Docker Cheat Sheet
1 Most Common Steps
1.1 Running a single service container
docker run --name nginx1 -d -p 8080:80 nginx:latest
# Access at: http://localhost:8080
# Management commands
docker ps # Check running containers
docker logs nginx1 # View logs
docker logs -f nginx1 # Follow logs (live)
docker stop nginx1 # Stop the container
docker start nginx1 # Start it again
docker restart nginx1 # Restart the container
docker rm nginx1 # Remove (must stop first)
docker rm -f nginx1 # Force remove (even if running)1.2 Running a single interactive container
# Start interactive container
docker run --name ubuntu1 -it ubuntu:latest bash
# Inside container
exit # Exit and STOP the container
# Restart stopped container
docker start ubuntu1 # Starts but doesn't attach
# Re-enter the container (SAFE - recommended)
docker exec -it ubuntu1 bash
exit # Exit but container keeps running ✅
# Alternative: attach to main process (RISKY)
docker attach ubuntu1
# Ctrl+P, Ctrl+Q # Detach safely
# OR
exit # This STOPS the container ❌
# Management
docker stop ubuntu1 # Stop
docker start ubuntu1 # Start again
docker restart ubuntu1 # Restart
docker rm ubuntu1 # Remove when done
docker rm -f ubuntu1 # Force remove1.3 Running multiple service containers (with docker compose)
# Directory structure:
# project/
# ├── compose.yaml
# ├── Dockerfile (optional, for custom apps)
# └── app files
# Example compose.yaml for multi-service app:
# services:
# web:
# build: .
# ports:
# - "5001:8080"
# networks:
# - app-net
# redis:
# image: redis:alpine
# networks:
# - app-net
# volumes:
# - redis-data:/data
# networks:
# app-net:
# volumes:
# redis-data:
# Start all services
docker compose up -d # Detached mode
docker compose up --build # Rebuild images first
docker compose up -d --build # Both
# View status
docker compose ps # List services
docker compose logs # View all logs
docker compose logs -f # Follow logs (live)
docker compose logs web # Logs for specific service
# Stop services
docker compose stop # Stop (containers remain)
docker compose down # Stop and remove containers/networks
docker compose down -v # Also remove volumes (deletes data!)
# Restart services
docker compose restart # Restart all
docker compose restart web # Restart specific service
# Execute commands in running services
docker compose exec web bash # Enter web service container
docker compose exec redis redis-cli # Run Redis CLI
# Scale services (run multiple instances)
docker compose up -d --scale web=3 # Run 3 instances of web
# View logs for specific service
docker compose logs -f web1.4 Running multiple interactive containers (manual approach)
# Create custom network for containers to communicate
docker network create mynetwork
# Start first interactive container
docker run -it --name ubuntu1 --network mynetwork ubuntu:latest bash
# Ctrl+P, Ctrl+Q to detach without stopping
# Start second interactive container
docker run -it --name ubuntu2 --network mynetwork ubuntu:latest bash
# Ctrl+P, Ctrl+Q to detach without stopping
# Containers can now communicate with each other
docker exec -it ubuntu1 bash
ping ubuntu2 # Can ping by container name!
exit
# Re-enter any container
docker exec -it ubuntu1 bash
docker exec -it ubuntu2 bash
# Clean up
docker stop ubuntu1 ubuntu2
docker rm ubuntu1 ubuntu2
docker network rm mynetwork
# NOTE: For multiple containers, docker compose is much easier!
# Use the "multiple service containers" approach above instead.1.5 Key differences summary
# Single container:
docker run # Manual, one container at a time
# Multiple containers:
docker compose # Automated, orchestrates multiple containers
# Interactive vs Service:
-it # Interactive (terminal access)
-d # Detached/daemon (runs in background)
# Entering containers:
docker exec -it # SAFE - new process, won't stop container
docker attach # RISKY - connects to main process, exit stops container
# Port mapping:
-p HOST:CONTAINER # 8080:80 means localhost:8080 → container:801.6 Editing the content of the image
docker exec -it webserver sh # enter the running container
vi views/home.pug # edit a file (example)
i # press i to enter insert mode in vi
Esc # press Esc to exit insert mode
:wq # type :wq and press Enter to save and exit vi
exit # exit the container1.7 Containerizing an Application with Docker Build (Only Build)
# Downloading
git clone ...
cd ...
# Building
docker build -t myprd:latest .
docker run --name myprd -d -p 8000:8000 myprd:latest
# Removing
docker stop myprd
docker rm myprd
docker rmi myprd:latest1.8 Containerizing an Application with Docker Compose (Build and Runs)
# Downloading
git clone ...
cd ...
# Building
docker init
docker compose up -d # to start the application in detached mode
docker compose logs # to view logs
# Removing
docker compose down # to stop and remove the application containers
docker rmi myapp:latest2 Commands
2.1 Working with Containers
docker ps # Check what's running
docker ps -a # See all (-a) containers including stopped
docker logs test # View container output
docker logs -f test # Follow (-f) logs in real-time
docker logs --tail 50 test # View last (--tail) 50 lines only
docker logs -t test # Show timestamps (-t) in logs
docker exec -it test bash # Enter container with interactive (-i) terminal (-t)
docker exec test ls -la /app # Run command without entering
docker exec -u root -it test bash # Enter as user (-u) root
docker attach test # Attach to main process (risky)
docker stop test # Stop a container
docker start test # Start a stopped container
docker restart test # Restart a container
docker rm test # Remove a stopped container
docker rm -f test # Force (-f) remove running container
docker rename old-name new-name # Rename a container
docker pause test # Pause a container
docker unpause test # Unpause a container2.2 Getting Container Information
docker inspect test # Full container details
docker inspect test | Select-String -Pattern 'Mounts' -Context 0,10 # Filter (PowerShell)
docker inspect test | grep -A 10 "Mounts" # Filter (Bash/Linux)
docker inspect --format='{{.NetworkSettings.IPAddress}}' test # Get IP using format (--format)
docker inspect --format='{{.State.Status}}' test # Get status using format
docker inspect --format='{{.State.ExitCode}}' test # Get exit code using format
docker port test # Show port mappings
docker top test # View running processes
docker stats # Monitor resource usage (all)
docker stats test # Monitor specific container
docker diff test # See filesystem changes
docker export test > test.tar # Export container filesystem2.3 Transferring Files
docker cp test:/app/file.txt ./file.txt # Copy (cp) from container to host
docker cp ./file.txt test:/app/file.txt # Copy (cp) from host to container2.4 Pulling Images from Registries
docker pull nginx:latest # Pull from official Docker Hub
docker pull nigelpoulton/tu-demo:v2 # Pull from user registry (Docker Hub)
docker pull ghcr.io/regclient/regsync:latest # Pull from GitHub Container Registry
docker pull myregistry.com/app:v1 # Pull from custom registry2.5 Managing Images
docker images # List all local images
docker build -t myapp:v1 . # Build with tag (-t) from current directory (.)
docker build -t myapp:v1 -f custom.Dockerfile . # Use file (-f) custom Dockerfile
docker tag myapp:v1 username/myapp:v1 # Tag for registry
docker rmi nginx:latest # Remove (rmi) an image
docker rmi $(docker images -q) # Remove all images using quiet (-q) output
docker image prune # Remove dangling images
docker image prune -a # Remove all (-a) unused images
docker history nginx:latest # View image layers2.6 Inspecting Images
docker inspect nginx:latest # Full image details
docker inspect nginx:latest | Select-String -Pattern 'Entrypoint' -Context 0,3 # Filter (PowerShell)
docker inspect nginx:latest | grep -A 3 "Entrypoint" # Filter (Bash/Linux)2.9 Running Containers - Service Mode
docker run --name test -d -p 8080:80 nginx:latest # Detached (-d) with port (-p) mapping
docker run -e NODE_ENV=production -d myapp:latest # With environment (-e) variable
docker run -v myvolume:/data -d nginx:latest # With volume (-v) mount
docker run --network mynetwork -d nginx:latest # On specific network (--network)
docker run --memory="512m" --cpus="1.5" -d nginx # With resource limits (--memory, --cpus)
docker run --restart unless-stopped -d nginx # With restart policy (--restart)2.10 Running Containers - Interactive Mode
docker run --name test -it ubuntu:latest bash # Interactive (-i) with terminal (-t)
docker run --rm -it ubuntu:latest bash # Auto-remove (--rm) after exit
docker run -it -v $(pwd):/app ubuntu bash # With volume (-v) current directory2.11 Working with Volumes
docker volume ls # List all volumes
docker volume create myvolume # Create a volume
docker volume inspect myvolume # View volume details
docker run --rm -v myvolume:/data alpine ls -la /data # Remove (--rm) after, volume (-v) mount
docker volume rm myvolume # Remove a volume
docker volume prune # Remove all unused volumes2.12 Mounting Volumes
docker run -v myvolume:/data -d nginx:latest # Named volume (-v)
docker run -v /host/path:/container/path -d nginx # Bind mount (-v) absolute path
docker run -v $(pwd):/app -d myapp # Bind mount (-v) current directory
docker run -v myvolume:/data:ro -d nginx # Read-only (ro) mount2.13 Working with Networks
docker network ls # List all networks
docker network create mynetwork # Create a network
docker network create --driver bridge mynetwork # With specific driver (--driver)
docker network inspect mynetwork # View network details
docker network connect mynetwork test # Connect container to network
docker network disconnect mynetwork test # Disconnect from network
docker network rm mynetwork # Remove a network
docker network prune # Remove all unused networks2.14 Starting Multi-Container Applications
docker compose up -d # Start all services detached (-d)
docker compose up -d --build # Start and rebuild (--build) images
docker compose up # Start in foreground (see logs)
docker compose up -d --scale web=3 # Start with scale (--scale) multiple instances2.15 Managing Running Services
docker compose ps # Check service status
docker compose logs # View all service logs
docker compose logs -f # Follow (-f) logs in real-time
docker compose logs web # Logs for specific service
docker compose logs -f --tail 50 web # Follow (-f) last (--tail) 50 lines
docker compose stop # Stop services (keep containers)
docker compose start # Start stopped services
docker compose restart # Restart all services
docker compose restart web # Restart specific service2.16 Working with Services
docker compose exec web bash # Execute (exec) in service container
docker compose exec redis redis-cli # Execute command in service
docker compose run web python manage.py migrate # Run one-off command (new container)2.17 Stopping Multi-Container Applications
docker compose down # Stop and remove containers/networks
docker compose down -v # Also remove volumes (-v) - DELETES DATA!2.18 Rebuilding Services
docker compose build # Rebuild all services
docker compose build web # Rebuild specific service
docker compose up -d --build web # Rebuild (--build) and restart detached (-d)2.19 Other Compose Operations
docker compose config # View merged configuration
docker compose pull # Pull all service images
docker compose push # Push all service images2.20 Scanning for Vulnerabilities
docker scout cves nginx:latest # Scan image for CVEs (vulnerabilities)
docker scout quickview nginx:latest # Quick security summary
docker scout compare --to nginx:1.25 nginx:latest # Compare (--to) versions
docker scout cves --format sarif nginx:latest # Output format (--format) SARIF2.21 Checking System Resources
docker system df # Show disk usage
docker system df -v # Verbose (-v) disk usage
docker info # Docker system information
docker version # Docker version details2.22 Cleaning Up Everything
docker system prune # Remove stopped containers, dangling images, unused networks
docker system prune -a # Also remove all (-a) unused images
docker system prune -a --volumes # Also remove volumes (--volumes) - DELETES DATA!
docker container prune # Remove stopped containers only
docker image prune # Remove dangling images only
docker image prune -a # Remove all (-a) unused images
docker volume prune # Remove unused volumes only
docker network prune # Remove unused networks only2.23 Bulk Container Operations
docker stop $(docker ps -q) # Stop all using quiet (-q) output
docker rm $(docker ps -a -q) # Remove all (-a) using quiet (-q) output
docker rmi $(docker images -q) # Remove all images using quiet (-q) output2.24 Troubleshooting and Debugging
docker events # Monitor Docker events (real-time)
docker wait test # Wait for container to stop
docker inspect --format='{{.HostConfig.Memory}}' test # Check memory limit using format (--format)
docker exec test env # View environment variables