โก Interactive Command Cheatsheet
Docker CLI Quick Reference
Essential daily Docker commands for building images, inspecting running containers, managing persistent volumes, and cleaning system resources.
๐จ Images
โถ๏ธ Containers
โ๏ธ Environment
๐พ Volumes
๐ Networking
๐ Compose
๐ Debug
๐งน Cleanup
๐จ Building & Image Management
| Command | Description |
|---|---|
| docker build -t myapp:latest . | Build an image tagged myapp:latest using the Dockerfile in the current directory. |
| docker build --no-cache -t myapp . | Force rebuilding all image layers without using cache. |
| docker images | List all locally cached Docker images with their tags and disk sizes. |
| docker rmi <image-id> | Remove one or more local images by ID or repository tag. |
โถ๏ธ Running & Container Lifecycle
| Command | Description |
|---|---|
| docker run -d -p 8080:80 --name web nginx | Run an Nginx container in background (-d), mapping host port 8080 to container 80. |
| docker run --rm -it alpine sh | Start an ephemeral interactive shell in Alpine and delete the container on exit (--rm). |
| docker ps -a | List all containers (both running and stopped/exited). |
| docker stop <container> && docker rm <container> | Gracefully stop a container (SIGTERM) and remove its container instance. |
๐ Debugging & Executing Commands
| Command | Description |
|---|---|
| docker exec -it <container> /bin/sh | Open an interactive shell inside an already running container for live debugging. |
| docker logs -f --tail 100 <container> | Follow (stream) the last 100 log lines from a container in real time. |
| docker stats | Display a live streaming data feed of CPU, RAM, and network I/O usage per container. |
| docker inspect <container> | Output detailed low-level JSON configuration (IP address, mounts, environment variables). |
โ๏ธ Environment Variables & Config
| Command | Description |
|---|---|
| docker run -e "NODE_ENV=production" -e "PORT=3000" myapp | Pass inline key-value environment variables directly to the container runtime. |
| docker run --env-file .env.production myapp | Load environment variables into the container from a local .env file. |
| docker exec <container> env | Print and inspect all environment variables currently active inside a running container. |
๐พ Volumes & Persistent Storage
| Command | Description |
|---|---|
| docker volume create pg_data | Create a named Docker volume managed independently of container lifecycles. |
| docker run -d -v pg_data:/var/lib/postgresql/data postgres | Mount a named volume to persist database records across container restarts. |
| docker run -d -v $(pwd):/app:ro myapp | Bind mount current directory from the host to /app in read-only mode (:ro). |
| docker volume ls && docker volume inspect <vol> | List all existing Docker volumes and display their host filesystem mountpoint. |
| docker volume rm <vol> | Delete a specific volume (must not be currently attached to any container). |
๐ Networking & Inter-Container DNS
| Command | Description |
|---|---|
| docker network create my-net | Create a user-defined bridge network providing automatic DNS resolution between container names. |
| docker run -d --network my-net --name db postgres | Run a container attached to my-net; reachable by other containers at hostname db. |
| docker network connect my-net <container> | Dynamically attach an existing container to a second network without restarting it. |
| docker network ls && docker network inspect <net> | List all networks (bridge, host, overlay) and view assigned IP addresses of connected containers. |
| docker port <container> | List all active port mappings between the container and the host machine. |
๐ Docker Compose (Multi-Container)
| Command | Description |
|---|---|
| docker compose up -d | Build, create, and start all services defined in docker-compose.yml in detached mode. |
| docker compose down -v | Stop and remove all containers, networks, and named volumes (-v) defined in the compose file. |
| docker compose logs -f <service> | Stream unified colored logs for a specific service or all compose services. |
| docker compose exec <service> sh | Execute an interactive shell inside a running compose service container. |
๐งน System Cleanup & Prune
| Command | Description |
|---|---|
| docker system prune -a --volumes | Nuclear cleanup: removes all stopped containers, unused networks, unreferenced images, and anonymous volumes. |
| docker image prune | Remove only dangling images (untagged images from intermediate builds). |
| docker volume prune | Remove all local volumes not used by at least one container. |