Essential Docker CLI Commands & Flags Used Daily in Production with Real-World Scenarios
· Sourabh G Kulkarni
Master essential Docker CLI commands and flags: buildx, exec, logs, cp, inspect, stats, networks, and prune for debugging and operating production systems.
Almost every engineer starts with the same four Docker commands: docker build, docker run, docker ps, and docker stop.
And when everything is working smoothly on your local machine, that basic quartet feels sufficient. But when a production database container begins thrashing CPU at 3:00 AM, a CI/CD build fails due to a missing environment variable, or an orphaned network interface locks up a host port, those basic commands fall short.
Operating containers in production requires command-line fluency: knowing how to filter JSON metadata using Go templates, stream live memory telemetry, surgically extract files from crashed containers, and debug virtual network bridges.
Here are the essential Docker CLI commands and flags used daily by DevOps and platform engineers, accompanied by real-world operational scenarios.
1. Surgical Inspection: Extracting Exact Metadata with --format
When debugging, running docker inspect <container> dumps hundreds of lines of raw JSON to your terminal. Manually scrolling through that wall of text to find a container's IP address or restart count is slow and inefficient.
Docker CLI natively supports Go template formatting via the -f / --format flag.
Scenario A: Extract the Container's Internal Bridge IP Address
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-app
# Output: 172.17.0.4
Scenario B: Extract Real-Time Health Check Status & Failure Logs
docker inspect -f '{{.State.Health.Status}} - Failed: {{.State.Health.FailingStreak}}' my-app
# Output: unhealthy - Failed: 4
Scenario C: Format docker ps into a Clean, Readable Table
Instead of truncating columns across narrow terminal windows, output only the fields you care about:
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"
2. Real-Time Resource Monitoring: docker stats
When a host server is under high load and you need to immediately identify which container is leaking memory or maxing out CPU cores:
docker stats --no-stream
Output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
a1b2c3d4e5f6 postgres 2.41% 412MiB / 8GiB 5.03% 12.4MB / 8.2MB 1.2GB / 400MB
f6e5d4c3b2a1 node-api 184.20% 3.81GiB / 4GiB 95.25% 84.1MB / 92.4MB 45MB / 12MB
- In one glance, you immediately see that
node-apihas pegged two CPU cores (184%) and is within 200MB of triggering an OOMKilled termination (95.25% MEM).
Pro Tip: Stream Specific Format for Scripting
docker stats --format "{{.Name}}: CPU {{.CPUPerc}} | MEM {{.MemPerc}}"
3. Production Log Troubleshooting: docker logs
Using raw docker logs <container> on a service that has been running for six months will dump gigabytes of text into your terminal, freezing your shell session.
The Essential Flags
-f(Follow): Stream live logs in real time (liketail -f).--tail 100: Print only the last 100 lines.--since 15m: Show only logs generated within the last 15 minutes (or pass an ISO timestamp2026-10-31T01:00:00).-t(Timestamps): Prepend an exact UTC timestamp to every log entry (vital for correlating events with Prometheus/Grafana alerts).
docker logs -f --tail 200 -t --since 30m payment-service
4. Disaster Recovery & File Extraction: docker cp
Imagine an application container crashes with a segmentation fault on startup. You need to inspect a crash dump file or a local SQLite database stored inside the container, but because the container is stopped, you cannot docker exec into it.
docker cp works even when the container is completely stopped:
# Copy a file from a stopped container to your host machine
docker cp crashed-container:/app/logs/crash.dump ./crash.dump
# Copy an updated configuration file from your host directly into a container
docker cp ./nginx.conf web-server:/etc/nginx/nginx.conf
5. Non-Destructive Interactive Debugging: docker exec
When entering a running container, you almost always need an interactive TTY session:
docker exec -it <container> /bin/sh
Scenario: Running as Root Inside an Unprivileged Container
For security reasons, production containers often run as non-root users (USER node or USER 1001). If you need to install debugging tools (like curl or tcpdump) to triage a live network issue, use the -u 0 (root) override flag:
docker exec -u 0 -it api-server apt-get update && apt-get install -y tcpdump
6. Network Triage: docker network inspect
When two containers fail to communicate over internal Docker bridge networks (e.g., web throws Connection Refused trying to reach db):
- Inspect the Bridge Network to Verify Both Containers Are Connected:
docker network inspect bridge - Create an Isolated Custom Bridge Network:
Containers on the default
bridgenetwork cannot resolve each other by container name. Custom bridge networks provide automatic internal DNS resolution:Now# 1. Create a custom network docker network create app-network # 2. Connect running containers on the fly docker network connect app-network web docker network connect app-network dbwebcan reachdbsimply by addressinghttp://db:5432.
7. Storage Hygiene: Safe vs Deep Prunes
# Level 1: Safe Prune (Removes stopped containers & dangling untagged images)
docker system prune
# Level 2: Comprehensive Prune (Also removes all unused images, not just dangling ones)
docker system prune -a
# Level 3: Deep BuildKit Cache Purge (Frees gigabytes of intermediate build layers)
docker builder prune -a --force
# Level 4: Volume Cleanup (CAUTION: Permanently purges unattached persistent volumes)
docker volume prune
Summary Reference Table
| Task | Command |
|---|---|
| Inspect Specific JSON Field | docker inspect -f '{{.State.Pid}}' <container> |
| Stream Tail of Recent Logs | docker logs -f --tail 100 --since 15m -t <container> |
| Live Resource Telemetry | docker stats --no-stream |
| Extract File from Stopped Container | docker cp <container>:/path/to/file ./local/ |
| Debug as Root User | docker exec -u 0 -it <container> /bin/sh |
| Connect Container to Network | docker network connect <network> <container> |
| Reclaim Host Disk Space | docker system prune -a && docker builder prune -a |