## Build, run and manage containers, images, volumes and Compose stacks

# Run an interactive container
docker run -it ubuntu:24.04 bash

# Run in the background with a name and a port
docker run -d --name web -p 8080:80 nginx

# Delete the container when it exits
docker run --rm alpine echo hello

# Environment variables
docker run -d --name db -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=app postgres:16

# Environment variables from a file
docker run --env-file .env myapp:latest

# Mount the current directory into the container
docker run --rm -v "$(pwd)":/app -w /app python:3.12 python script.py

# Named volume for persistent data
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16

# Read-only config mount
docker run -d -v "$(pwd)/nginx.conf":/etc/nginx/nginx.conf:ro nginx

# Restart automatically unless stopped by hand
docker run -d --restart unless-stopped --name redis redis:7

# Limit memory and CPU
docker run -d --memory=512m --cpus=1.5 myapp

# Put containers on a shared network (reach each other by name)
docker network create appnet
docker run -d --network appnet --name db postgres:16

# Running containers / all containers
docker ps
docker ps -a

# Custom table output
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

# Follow the last 100 log lines
docker logs -f --tail 100 web

# Logs from the last 30 minutes
docker logs --since 30m web

# Open a shell in a running container
docker exec -it web sh

# Shell as root
docker exec -u 0 -it web bash

# Stop, start, restart
docker stop web
docker start web
docker restart web

# Force-remove a container
docker rm -f web

# Build an image
docker build -t myapp:1.0 .

# Build with another Dockerfile and a build argument
docker build -f docker/Dockerfile --build-arg VERSION=1.0 -t myapp:1.0 .

# Build without cache
docker build --no-cache -t myapp:latest .

# Build for several CPU architectures and push
docker buildx build --platform linux/amd64,linux/arm64 -t user/myapp:1.0 --push .

# List images
docker images

# Tag and push to a registry
docker tag myapp:1.0 user/myapp:1.0
docker push user/myapp:1.0

# Log in to a registry
docker login ghcr.io -u USERNAME

# Container IP address
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

# Health check status
docker inspect --format '{{.State.Health.Status}}' web

# Live CPU and memory usage (one snapshot)
docker stats --no-stream

# Processes inside a container
docker top web

# Copy a file out of a container
docker cp web:/etc/nginx/nginx.conf ./nginx.conf

# Image layers and their sizes
docker history myapp:1.0

# Export an image to a file / import it on another machine
docker save myapp:1.0 | gzip > myapp.tar.gz
gunzip -c myapp.tar.gz | docker load

# Dump a database from a container
docker exec db pg_dump -U postgres app > app.sql

# Disk space used by Docker
docker system df

# Remove unused images
docker image prune -a

# Remove everything unused, including volumes (careful)
docker system prune -a --volumes

# Compose: start the stack in the background
docker compose up -d

# Compose: rebuild images and start
docker compose up -d --build

# Compose: follow logs of one service
docker compose logs -f api

# Compose: run a command in a service
docker compose exec db psql -U postgres

# Compose: stop and remove containers and volumes
docker compose down -v
