Docker revolutionizes software deployment by packaging applications and their dependencies into standardized, lightweight containers. Mastering Docker CLI and Docker Compose V2 enables developers to achieve environment parity between local development and cloud production clusters.
Key Takeaways
- Use `docker run -d -p <host>:<container>` to launch background containerized applications.
- Leverage Docker Compose V2 (`docker compose up -d`) to orchestrate multi-container services.
- Write multi-stage Dockerfiles to minimize production image footprints and eliminate dev dependencies.
- Run `docker system prune -a --volumes` to clean up dangling images, unused volumes, and stopped containers.
How do you build, inspect, tag, and manage Docker images?
Docker images are read-only templates containing application source code, runtime environments, and system libraries.
Docker Image Operations
| Command | Action |
|---|---|
docker build -t myapp . | Build an image from Dockerfile in current directory |
docker build -t myapp:1.0 . | Build an image with a specific version tag |
docker images | List all locally cached Docker images |
docker rmi myapp | Remove a local image |
docker rmi $(docker images -q) | Remove all local images |
docker pull nginx | Download an image from Docker Hub |
docker tag myapp myapp:v1 | Tag an image with a new repository/version tag |
docker push user/myapp:v1 | Upload an image to Docker Hub or remote registry |
docker history nginx | Inspect layer creation history of an image |
docker inspect nginx | Display full JSON metadata for an image |
How do you run, manage, inspect, and debug Docker containers?
Containers are isolated, runnable instances of Docker images that execute isolated processes on the host OS kernel.
Container Execution (docker run)
| Command | Action |
|---|---|
docker run nginx | Run a container in the foreground |
docker run -d nginx | Run a container detached in the background |
docker run -d -p 8080:80 nginx | Map host port 8080 to container port 80 |
docker run -d --name web nginx | Assign a custom container name |
docker run -it ubuntu bash | Launch container with interactive pseudo-TTY terminal |
docker run --rm nginx | Automatically remove container when it stops |
docker run -e VAR=value nginx | Inject an environment variable |
docker run --env-file .env nginx | Load environment variables from a .env file |
docker run -v /host:/container nginx | Bind mount a local host directory into container |
docker run -v myvolume:/data nginx | Attach a named Docker volume |
docker run --network mynet nginx | Connect container to a specific network bridge |
docker run --restart always nginx | Automatically restart container if it crashes |
docker run -m 512m --cpus 1.5 nginx | Restrict memory to 512MB and CPU to 1.5 cores |
Container Lifecycle & Management
| Command | Action |
|---|---|
docker ps | List currently running containers |
docker ps -a | List all containers (running and stopped) |
docker stop web | Gracefully stop a running container (SIGTERM) |
docker kill web | Forcefully terminate a container immediately (SIGKILL) |
docker start web | Start a stopped container |
docker restart web | Restart a container |
docker rm web | Remove a stopped container |
docker rm -f web | Force remove a running container |
docker rm $(docker ps -aq) | Remove all stopped containers across system |
docker rename old_name new_name | Rename an existing container |
docker pause web / docker unpause web | Pause / resume all processes inside container |
Container Inspection & Debugging
| Command | Action |
|---|---|
docker logs web | Fetch container stdout/stderr log output |
docker logs -f web | Follow container logs in real time |
docker logs --tail 100 web | Display last 100 log entries |
docker logs --since 1h web | Display logs generated within the last hour |
docker inspect web | Inspect full container state and JSON configuration |
docker stats | Monitor live stream of container CPU, RAM, and network I/O |
docker top web | Display active processes running inside container |
docker diff web | Inspect modified files in container filesystem |
docker port web | List container port mappings |
docker exec -it web bash | Open an interactive shell inside running container |
docker cp web:/app/file.log ./ | Copy file from container to local host path |
docker cp ./config.json web:/app/ | Copy file from local host path into container |
How do you manage Docker Volumes, Networks, and Disk Cleanup?
Volumes persist application state beyond container lifecycles, while custom bridge networks facilitate inter-container communication.
Volumes & Networks
| Command | Action |
|---|---|
docker volume ls | List all active Docker volumes |
docker volume create mydata | Create a new named volume |
docker volume inspect mydata | Display volume mount path and metadata |
docker volume rm mydata | Delete a specific volume |
docker volume prune | Delete all unused volumes |
docker network ls | List all Docker networks |
docker network create mynet | Create a custom bridge network |
docker network connect mynet web | Attach a running container to a network |
docker network disconnect mynet web | Detach a container from a network |
docker network rm mynet | Delete a custom network |
docker network prune | Delete all unused networks |
System Cleanup & Pruning
| Command | Action |
|---|---|
docker system df | Display Docker disk space consumption |
docker system prune | Remove stopped containers, dangling images, and unused networks |
docker system prune -a | Remove stopped containers, unused networks, and ALL unreferenced images |
docker system prune -a --volumes | Complete Reset: Remove all unreferenced images, containers, and volumes |
How do you manage multi-container apps with Docker Compose V2?
Docker Compose orchestrates multi-container applications defined in a declarative docker-compose.yml file.
Common Compose V2 Commands
| Command | Action |
|---|---|
docker compose up | Build, create, and start services in foreground |
docker compose up -d | Start services in background (detached mode) |
docker compose up --build | Force rebuild of custom container images before starting |
docker compose down | Stop and remove container services and networks |
docker compose down -v | Stop services and remove attached volumes |
docker compose ps | List status of all service containers |
docker compose logs -f | Follow logs across all services |
docker compose exec web bash | Execute shell inside a running service container |
docker compose run web bash | Launch a one-off container command |
Production docker-compose.yml Template
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://user:pass@db:5432/mydb
volumes:
- ./uploads:/app/uploads
depends_on:
- db
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
restart: unless-stopped
volumes:
pgdata:What are best practices for multi-stage Dockerfiles and one-liner tricks?
Multi-stage builds decouple build-time compilation tools from runtime environments, yielding smaller, more secure container images.
Multi-Stage Dockerfile (Dockerfile)
# Stage 1: Build compilation
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: Production runtime
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
RUN npm ci --omit=dev
EXPOSE 3000
CMD ["node", "dist/server.js"]Essential Docker One-Liners
# Stop all currently running containers
docker stop $(docker ps -q)
# Remove all stopped containers
docker rm $(docker ps -aq)
# Get a container's IP address
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
# Rebuild and restart a single compose service
docker compose up -d --build web
# Export an image to a tar archive
docker save myapp:v1 > myapp.tar
# Import an image from a tar archive
docker load < myapp.tarFrequently Asked Questions
What is the difference between docker run and docker exec?
docker run creates and starts a brand-new container from an image. docker exec executes a command inside an already running container.
What is the difference between Docker Compose V1 and V2?
Docker Compose V2 is written in Go and integrated directly into the Docker CLI (docker compose), replacing the legacy Python-based standalone docker-compose binary.
What to Read Next
- Linux Bash Cheat Sheet: Commands & Scripting — Terminal commands for server management.
- Nginx Cheat Sheet: Reverse Proxy & SSL — Reverse proxying container services.
- Kubernetes Cheat Sheet: Every kubectl Command — Orchestrate cloud container workloads.



