This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| wiki:ai:docker_cheatsheet [2026/01/09 18:12] – bgourley | wiki:ai:docker_cheatsheet [2026/01/21 22:00] (current) – bgourley | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | **Docker Basics – Command Reference** | + | ======Docker Basics – Command Reference====== |
| - | **Docker Installation and Health Checks** | + | ====Docker Installation and Health Checks==== |
| **docker version** | **docker version** | ||
| Line 30: | Line 30: | ||
| docker info | docker info | ||
| - | **Working With Containers** | + | ====Working With Containers==== |
| **docker run** | **docker run** | ||
| Line 97: | Line 97: | ||
| docker inspect my-container | docker inspect my-container | ||
| - | **Executing Commands in Running Containers** | + | ====Executing Commands in Running Containers==== |
| **docker exec** | **docker exec** | ||
| Line 114: | Line 114: | ||
| docker exec my-container ls /etc | docker exec my-container ls /etc | ||
| - | **Logs and Runtime Monitoring** | + | ====Logs and Runtime Monitoring==== |
| **docker logs** | **docker logs** | ||
| Line 140: | Line 140: | ||
| docker stats my-container | docker stats my-container | ||
| - | **Images and Registries** | + | ====Images and Registries==== |
| **docker pull** | **docker pull** | ||
| Line 183: | Line 183: | ||
| You must authenticate with the registry using docker login. | You must authenticate with the registry using docker login. | ||
| - | **Cleanup and Disk Management** | + | ====Cleanup and Disk Management==== |
| **docker system df** | **docker system df** | ||
| Line 227: | Line 227: | ||
| This removes all unused images and volumes and should be used with caution. | This removes all unused images and volumes and should be used with caution. | ||
| - | **Docker Contexts** | + | ====Docker Contexts==== |
| **docker context ls** | **docker context ls** | ||
| Line 247: | Line 247: | ||
| docker context use desktop-linux | docker context use desktop-linux | ||
| - | **Docker Networking Basics** | + | ======Docker Networking Basics====== |
| - | **Docker Network Types** | + | ====Docker Network Types==== |
| **Default bridge network** | **Default bridge network** | ||
| Line 288: | Line 288: | ||
| docker run --rm --network none alpine sh | docker run --rm --network none alpine sh | ||
| - | **Managing Docker Networks** | + | ====Managing Docker Networks==== |
| **docker network ls** | **docker network ls** | ||
| Line 329: | Line 329: | ||
| docker network rm ai-net | docker network rm ai-net | ||
| - | **Running Containers on Networks** | + | ====Running Containers on Networks==== |
| **Run container on a specific network** | **Run container on a specific network** | ||
| Line 393: | Line 393: | ||
| Port publishing is not required for container-to-container communication on the same Docker network. | Port publishing is not required for container-to-container communication on the same Docker network. | ||
| - | **Container-to-Container Communication** | + | ====Container-to-Container Communication==== |
| **Same network communication** | **Same network communication** | ||
| Line 421: | Line 421: | ||
| docker run --rm -it --network other-net nicolaka/ | docker run --rm -it --network other-net nicolaka/ | ||
| - | **Networking Diagnostics and Debugging** | + | ====Networking Diagnostics and Debugging==== |
| **Inspect container network settings** | **Inspect container network settings** | ||
| Line 441: | Line 441: | ||
| docker run --rm -it --network ai-net nicolaka/ | docker run --rm -it --network ai-net nicolaka/ | ||
| - | **Cleanup Networking Resources** | + | ====Cleanup Networking Resources==== |
| **Remove containers** | **Remove containers** | ||
| Line 450: | Line 450: | ||
| docker network rm ai-net test-net other-net | docker network rm ai-net test-net other-net | ||
| + | |||
| + | ======Docker Volumes and Storage====== | ||
| + | |||
| + | ====Volumes vs Bind Mounts vs tmpfs==== | ||
| + | |||
| + | **Concept Summary** | ||
| + | |||
| + | Named volume | ||
| + | |||
| + | * Managed by Docker | ||
| + | * Lives in Docker’s storage area | ||
| + | * Best for persistent application data | ||
| + | |||
| + | Bind mount | ||
| + | |||
| + | * Maps a host directory into the container | ||
| + | * Best for development and live code editing | ||
| + | |||
| + | tmpfs | ||
| + | |||
| + | * In-memory filesystem | ||
| + | * Data is lost when the container stops | ||
| + | * Best for temporary or sensitive data | ||
| + | |||
| + | **Named volume example** | ||
| + | |||
| + | Create a named volume: | ||
| + | |||
| + | docker volume create myvol | ||
| + | |||
| + | docker volume ls | ||
| + | |||
| + | docker volume inspect myvol | ||
| + | |||
| + | Run a container using the volume: | ||
| + | |||
| + | docker run --rm -it -v myvol:/data alpine sh | ||
| + | |||
| + | Inside the container: | ||
| + | |||
| + | echo "hello from volume" | ||
| + | |||
| + | ls /data | ||
| + | |||
| + | exit | ||
| + | |||
| + | Run another container with the same volume: | ||
| + | |||
| + | docker run --rm -it -v myvol:/data alpine sh | ||
| + | |||
| + | Inside: | ||
| + | |||
| + | cat / | ||
| + | |||
| + | exit | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * The volume persists after the first container exits. | ||
| + | * A second container can read the same data. | ||
| + | * Data is independent of any specific container. | ||
| + | |||
| + | **Bind mount example (host directory)** | ||
| + | |||
| + | Create a directory on the host: | ||
| + | |||
| + | mkdir -p ~/ | ||
| + | |||
| + | echo "hello from host" > ~/ | ||
| + | |||
| + | Run a container with a bind mount: | ||
| + | |||
| + | docker run --rm -it \ | ||
| + | |||
| + | -v ~/ | ||
| + | |||
| + | alpine sh | ||
| + | |||
| + | Inside the container: | ||
| + | |||
| + | ls /data | ||
| + | |||
| + | echo " | ||
| + | |||
| + | exit | ||
| + | |||
| + | On the host: | ||
| + | |||
| + | ls ~/ | ||
| + | |||
| + | cat ~/ | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * The container sees and modifies the host filesystem directly. | ||
| + | * Changes on the host and in the container are immediately visible. | ||
| + | * No rebuild is required to reflect code or data changes. | ||
| + | |||
| + | **tmpfs example (in-memory)** | ||
| + | |||
| + | Run a container with tmpfs: | ||
| + | |||
| + | docker run --rm -it \ | ||
| + | |||
| + | --tmpfs / | ||
| + | |||
| + | alpine sh | ||
| + | |||
| + | Inside: | ||
| + | |||
| + | echo "temp data" > / | ||
| + | |||
| + | ls /data | ||
| + | |||
| + | exit | ||
| + | |||
| + | Run a new container: | ||
| + | |||
| + | docker run --rm -it --tmpfs /data alpine sh -c "ls /data || echo empty" | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * Data stored in tmpfs disappears when the container stops. | ||
| + | * Data never touches disk. | ||
| + | * Storage is purely in memory. | ||
| + | |||
| + | ====Create and Manage Named Volumes==== | ||
| + | |||
| + | **Create and list volumes** | ||
| + | |||
| + | docker volume create ai-data | ||
| + | |||
| + | docker volume ls | ||
| + | |||
| + | **Inspect volume metadata** | ||
| + | |||
| + | docker volume inspect ai-data | ||
| + | |||
| + | Look for: | ||
| + | |||
| + | * Driver type (usually local) | ||
| + | * Mountpoint inside Docker’s storage | ||
| + | |||
| + | **Use a volume and verify persistence** | ||
| + | |||
| + | Write data into the volume: | ||
| + | |||
| + | docker run --rm -it -v ai-data:/ | ||
| + | |||
| + | Inside: | ||
| + | |||
| + | echo "model checkpoint v1" > / | ||
| + | |||
| + | exit | ||
| + | |||
| + | Verify persistence: | ||
| + | |||
| + | docker run --rm -it -v ai-data:/ | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * Data written by one container is visible to future containers. | ||
| + | * Volumes persist beyond container lifetimes. | ||
| + | |||
| + | ====Use Bind Mounts for Development==== | ||
| + | |||
| + | **Create a simple development file** | ||
| + | |||
| + | On the host: | ||
| + | |||
| + | mkdir -p ~/bind-dev | ||
| + | |||
| + | cat > ~/ | ||
| + | |||
| + | print(" | ||
| + | |||
| + | EOF | ||
| + | |||
| + | Run a Python container with bind mount: | ||
| + | |||
| + | docker run --rm -it \ | ||
| + | |||
| + | -v ~/ | ||
| + | |||
| + | python: | ||
| + | |||
| + | python /app/app.py | ||
| + | |||
| + | Edit the file on the host: | ||
| + | |||
| + | echo ' | ||
| + | |||
| + | Re-run the container: | ||
| + | |||
| + | docker run --rm -it \ | ||
| + | |||
| + | -v ~/ | ||
| + | |||
| + | python: | ||
| + | |||
| + | python /app/app.py | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * The container runs code directly from the host filesystem. | ||
| + | * Code changes do not require rebuilding an image. | ||
| + | * Ideal for iterative development workflows. | ||
| + | |||
| + | ====Share Volumes Between Containers==== | ||
| + | |||
| + | **Create a shared volume** | ||
| + | |||
| + | docker volume create shared-vol | ||
| + | |||
| + | **Writer container** | ||
| + | |||
| + | Run: | ||
| + | |||
| + | docker run --rm -it -v shared-vol:/ | ||
| + | |||
| + | Inside: | ||
| + | |||
| + | echo " | ||
| + | |||
| + | exit | ||
| + | |||
| + | **Reader container** | ||
| + | |||
| + | Run: | ||
| + | |||
| + | docker run --rm -it -v shared-vol:/ | ||
| + | |||
| + | Inside: | ||
| + | |||
| + | cat / | ||
| + | |||
| + | exit | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * Multiple containers can mount the same volume. | ||
| + | * Data written by one container is immediately available to others. | ||
| + | * Enables multi-stage pipelines and producer–consumer patterns. | ||
| + | |||
| + | ====Back Up and Restore Volume Data==== | ||
| + | |||
| + | **Back up a volume to a tar archive** | ||
| + | |||
| + | Create a backup: | ||
| + | |||
| + | docker run --rm \ | ||
| + | |||
| + | -v ai-data:/ | ||
| + | |||
| + | -v $(pwd):/ | ||
| + | |||
| + | alpine \ | ||
| + | |||
| + | tar czf / | ||
| + | |||
| + | Verify: | ||
| + | |||
| + | ls ai-data-backup.tar.gz | ||
| + | |||
| + | What this does | ||
| + | |||
| + | * Mounts the volume at /data | ||
| + | * Mounts the current host directory at /backup | ||
| + | * Archives all volume contents into a file on the host | ||
| + | |||
| + | **Simulate data loss** | ||
| + | |||
| + | Delete and recreate the volume: | ||
| + | |||
| + | docker volume rm ai-data | ||
| + | |||
| + | docker volume create ai-data | ||
| + | |||
| + | Check it is empty: | ||
| + | |||
| + | docker run --rm -it -v ai-data:/ | ||
| + | |||
| + | **Restore from backup** | ||
| + | |||
| + | Restore the data: | ||
| + | |||
| + | docker run --rm \ | ||
| + | |||
| + | -v ai-data:/ | ||
| + | |||
| + | -v $(pwd):/ | ||
| + | |||
| + | alpine \ | ||
| + | |||
| + | tar xzf / | ||
| + | |||
| + | Verify: | ||
| + | |||
| + | docker run --rm -it -v ai-data:/ | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * Volume data can be fully backed up and restored. | ||
| + | * Volumes are portable across machines and environments. | ||
| + | |||
| + | ====Clean Up Unused Volumes==== | ||
| + | |||
| + | **List volumes** | ||
| + | |||
| + | docker volume ls | ||
| + | |||
| + | **Remove specific volumes** | ||
| + | |||
| + | docker volume rm myvol shared-vol ai-data | ||
| + | |||
| + | **Remove all unused volumes** | ||
| + | |||
| + | docker volume prune | ||
| + | |||
| + | **Inspect disk usage** | ||
| + | |||
| + | docker system df -v | ||
| + | |||
| + | What this demonstrates | ||
| + | |||
| + | * Volumes consume real disk space. | ||
| + | * Unused volumes accumulate over time. | ||
| + | * Regular cleanup prevents silent disk exhaustion. | ||
| + | ======Docker Compose====== | ||
| + | |||
| + | ====Overview==== | ||
| + | |||
| + | This reference documents the core Docker Compose commands used to: | ||
| + | |||
| + | * Build and run multi-container applications | ||
| + | * Manage service dependencies | ||
| + | * Configure health checks | ||
| + | * Use environment files | ||
| + | * Control application lifecycle | ||
| + | * Scale services horizontally | ||
| + | |||
| + | All commands assume: | ||
| + | |||
| + | * macOS with Docker Desktop | ||
| + | * Docker Compose v2 (docker compose) | ||
| + | * A project directory containing docker-compose.yml | ||
| + | |||
| + | ====Project Setup Commands==== | ||
| + | |||
| + | **mkdir and cd** | ||
| + | |||
| + | Purpose\\ | ||
| + | Creates and enters a new project directory for a Compose application. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | mkdir compose-lab && cd compose-lab | ||
| + | |||
| + | mkdir app | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Creates a folder to hold all Compose-related files | ||
| + | * Groups all containers, networks, and volumes under a single project name | ||
| + | * Makes lifecycle management easier | ||
| + | |||
| + | ====Docker Compose Lifecycle Commands==== | ||
| + | |||
| + | **docker compose up** | ||
| + | |||
| + | Purpose\\ | ||
| + | Builds images (if needed) and starts all services defined in docker-compose.yml. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose up | ||
| + | |||
| + | docker compose up -d | ||
| + | |||
| + | docker compose up -d --build | ||
| + | |||
| + | Options | ||
| + | |||
| + | * -d runs services in the background (detached mode) | ||
| + | * --build forces a rebuild of images before starting | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Creates a default network for the project | ||
| + | * Creates named volumes if missing | ||
| + | * Builds images for services with build: | ||
| + | * Starts containers in dependency order | ||
| + | * Attaches logs to the terminal (unless -d is used) | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Initial startup of the stack | ||
| + | * After code or Dockerfile changes | ||
| + | * For local testing and dev environments | ||
| + | |||
| + | **docker compose down** | ||
| + | |||
| + | Purpose\\ | ||
| + | Stops and removes all services in the Compose project. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose down | ||
| + | |||
| + | docker compose down -v | ||
| + | |||
| + | docker compose down --rmi local -v | ||
| + | |||
| + | Options | ||
| + | |||
| + | * -v removes named volumes (destroys persistent data) | ||
| + | * --rmi local removes images built by Compose | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Stops all running containers | ||
| + | * Deletes containers | ||
| + | * Deletes the project network | ||
| + | * Optionally deletes volumes and images | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Resetting the environment | ||
| + | * Freeing system resources | ||
| + | * Recreating a clean stack | ||
| + | |||
| + | **docker compose ps** | ||
| + | |||
| + | Purpose\\ | ||
| + | Lists the current status of services in the Compose project. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose ps | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Shows container names | ||
| + | * Displays service states (running, exited, unhealthy) | ||
| + | * Shows port mappings | ||
| + | * Displays health status | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Verify everything is running | ||
| + | * Debug startup failures | ||
| + | * Check healthcheck status | ||
| + | |||
| + | **docker compose logs** | ||
| + | |||
| + | Purpose\\ | ||
| + | Displays logs from all services in the project. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose logs | ||
| + | |||
| + | docker compose logs -f | ||
| + | |||
| + | docker compose logs api | ||
| + | |||
| + | Options | ||
| + | |||
| + | * -f follows logs in real time | ||
| + | * api limits output to a single service | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Aggregates stdout and stderr from all containers | ||
| + | * Prefixes logs with service names | ||
| + | * Streams logs live when -f is used | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Debug startup issues | ||
| + | * Monitor API or database activity | ||
| + | * Investigate crashes or errors | ||
| + | |||
| + | ====Build and Image Management==== | ||
| + | |||
| + | **docker compose build** | ||
| + | |||
| + | Purpose\\ | ||
| + | Builds images for services that specify a build: section. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose build | ||
| + | |||
| + | docker compose build api | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Runs docker build for each service image | ||
| + | * Tags images with project-specific names | ||
| + | * Caches build layers between runs | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Pre-build images before starting services | ||
| + | * Debug Dockerfile changes | ||
| + | * Speed up later docker compose up runs | ||
| + | |||
| + | ====Service Interaction Commands==== | ||
| + | |||
| + | **docker compose exec** | ||
| + | |||
| + | Purpose\\ | ||
| + | Runs a command inside a running service container. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose exec api sh | ||
| + | |||
| + | docker compose exec db psql -U composeuser compose_db | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Executes a command in the target container | ||
| + | * Attaches stdin/ | ||
| + | * Uses the existing running container | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Debug inside containers | ||
| + | * Inspect environment variables | ||
| + | * Run admin tools (psql, bash, python) | ||
| + | |||
| + | **docker compose stop** | ||
| + | |||
| + | Purpose\\ | ||
| + | Stops services without removing containers. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose stop | ||
| + | |||
| + | docker compose stop db | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Sends a stop signal to containers | ||
| + | * Keeps containers, networks, and volumes intact | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Temporarily pausing services | ||
| + | * Simulating outages | ||
| + | * Preserving state for later restart | ||
| + | |||
| + | **docker compose start** | ||
| + | |||
| + | Purpose\\ | ||
| + | Restarts stopped services. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose start | ||
| + | |||
| + | docker compose start db | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Restarts previously stopped containers | ||
| + | * Preserves container filesystem and volumes | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Resuming paused services | ||
| + | * Testing recovery behavior | ||
| + | * Bringing a stack back online | ||
| + | |||
| + | ====Health and Inspection Commands==== | ||
| + | |||
| + | **docker inspect** | ||
| + | |||
| + | Purpose\\ | ||
| + | Displays low-level metadata about a container. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker inspect compose-db | ||
| + | |||
| + | docker inspect compose-api | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Shows container config, networks, volumes | ||
| + | * Displays health status and logs | ||
| + | * Reveals failure reasons for unhealthy services | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Debug healthcheck failures | ||
| + | * Inspect network wiring | ||
| + | * Validate volume mounts | ||
| + | |||
| + | **docker compose exec env** | ||
| + | |||
| + | Purpose\\ | ||
| + | Displays environment variables inside a container. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose exec api env | sort | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Prints all environment variables | ||
| + | * Confirms .env and env_file values are injected | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Validate runtime configuration | ||
| + | * Debug missing credentials | ||
| + | * Confirm DB host/port values | ||
| + | |||
| + | ====Service Scaling==== | ||
| + | |||
| + | **docker compose up --scale** | ||
| + | |||
| + | Purpose\\ | ||
| + | Runs multiple instances of a service. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose up -d --scale api=3 | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Starts additional containers for the service | ||
| + | * Names them sequentially (api-1, api-2, api-3) | ||
| + | * Attaches all replicas to the same network | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Simulate horizontal scaling | ||
| + | * Test concurrency handling | ||
| + | * Prepare for load balancing setups | ||
| + | |||
| + | Important limitation\\ | ||
| + | Compose does not automatically load-balance traffic across replicas.\\ | ||
| + | A reverse proxy (nginx, Traefik) is required for real traffic distribution. | ||
| + | |||
| + | ====Networking and Port Mapping==== | ||
| + | |||
| + | **curl http: | ||
| + | |||
| + | Purpose\\ | ||
| + | Tests connectivity from the host into a containerized service. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | curl http: | ||
| + | |||
| + | curl http: | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Sends HTTP request to host port 8080 | ||
| + | * Docker forwards traffic into the API container | ||
| + | * Validates networking and service health | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Smoke testing APIs | ||
| + | * Validating port publishing | ||
| + | * Debugging networking issues | ||
| + | |||
| + | ====Cleanup and Reset Commands==== | ||
| + | |||
| + | **docker compose down -v** | ||
| + | |||
| + | Purpose\\ | ||
| + | Destroys the entire Compose environment including volumes. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose down -v | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Stops all containers | ||
| + | * Deletes containers | ||
| + | * Deletes networks | ||
| + | * Deletes named volumes | ||
| + | * Wipes database and persistent state | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Full environment reset | ||
| + | * Freeing disk space | ||
| + | * Starting fresh | ||
| + | |||
| + | **docker compose down --rmi local -v** | ||
| + | |||
| + | Purpose\\ | ||
| + | Removes everything created by Compose including images. | ||
| + | |||
| + | Usage | ||
| + | |||
| + | docker compose down --rmi local -v | ||
| + | |||
| + | What it does | ||
| + | |||
| + | * Stops services | ||
| + | * Deletes containers | ||
| + | * Deletes networks | ||
| + | * Deletes volumes | ||
| + | * Deletes built images | ||
| + | |||
| + | When to use | ||
| + | |||
| + | * Total teardown | ||
| + | * Reclaiming disk space | ||
| + | * Forcing clean rebuilds | ||