Docker is a cornerstone of modern DevOps pipelines. Below is a curated list of 30 real-world Docker command use cases, each tailored to solve a specific problem — from builds and tagging to logs, security, and performance.
🔨 Build & Tagging Strategies
1. Build with Environment-Specific Dockerfile
docker build -t myapp:1.0 -f docker/Dockerfile.prod .
Use Case: Use different Dockerfiles for dev and prod.
2. Force a Clean Build
docker build --no-cache -t cleanbuild:v2 .
Use Case: Skip the cache if stale layers are causing bugs.
3. Pass Build Arguments
docker build --build-arg NODE_ENV=production -t nodeapp:prod .
Use Case: Change environment context at build time.
4. Tag Image with Git Commit Hash
docker build -t registry.local/myorg/backend:$(git rev-parse --short HEAD) .
Use Case: CI pipelines for traceable image versions.
5. Add a Date-Based Tag
docker tag myapp:latest myregistry.com/myapp:release-$(date +%Y%m%d)
Use Case: Organize image snapshots chronologically.
🚀 Runtime & Execution Control
6. Run with Resource Limits
docker run -it --rm --memory="512m" --cpus="1.5" myapp:prod
Use Case: Simulate constrained environments.
7. Run as Non-Root User
docker run -u 1001:1001 -it myapp bash
Use Case: Improve container security posture.
8. Read-Only Container Filesystem
docker run --read-only -v /tmp --tmpfs /tmp myapp
Use Case: Prevent runtime file tampering.
9. Auto-Restart on Failure
docker run -d --restart=on-failure:3 alpine sh -c "sleep 1 && exit 1"
Use Case: Recover from intermittent app crashes.
10. Log Rotation
docker run -d \
--log-driver=json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name mylogger \
alpine sh -c "while true; do echo Hello >> /dev/stdout; sleep 0.01; done"
Use Case: Prevent disk space bloat from logs.
✅ Health, Debug & Monitoring
11. Dockerfile Healthcheck
HEALTHCHECK CMD curl --fail http://localhost:8080/health || exit 1
Use Case: Auto-healing for failed containers in Swarm/K8s.
12. Tail Logs Live
docker logs -f --tail 50 mycontainer
Use Case: Live-debug deployment issues.
13. Inspect a Running Process
docker exec -it mycontainer sh -c "ps aux | grep java"
Use Case: Check internal processes like Java.
14. Apply Multiple Constraints
docker run --pids-limit 100 --memory=256m --read-only myapp
Use Case: Secure sensitive apps with runtime limits.
15. Bind Mount Host Logs
docker run -v $(pwd)/logs:/var/log/nginx nginx
Use Case: Live access container logs from host.
16. Docker Compose Logs
docker-compose logs -f backend
Use Case: Trace backend service behavior.
🧹 Cleanup & Housekeeping
17. Clean Everything
docker system prune -a --volumes -f
Use Case: Free space after CI builds.
18. Remove Dangling Images
docker rmi $(docker images -f "dangling=true" -q)
Use Case: Keep only useful, tagged images.
19. Remove All Containers
docker rm -f $(docker ps -aq)
Use Case: Reset Docker to a clean state.
20. List Images Since a Base
docker image ls -f "since=ubuntu"
Use Case: Identify recent images in a dev session.
21. Rename a Container
docker rename oldcontainer newcontainer
Use Case: Avoid name collisions or standardize.
22. Always Restart Policy
docker update --restart=always mycontainer
Use Case: Ensure critical services auto-restart.
23. Pause & Resume Container
docker pause mycontainer && docker unpause mycontainer
Use Case: Suspend during host maintenance.
24. View Filesystem Changes
docker diff mycontainer
Use Case: Debug unexpected state changes.
25. Export Image to TAR
docker save myapp > myapp.tar
Use Case: Share images in air-gapped environments.
26. Import Image from TAR
docker load < myapp.tar
Use Case: Restore saved images offline.
27. Copy Logs from Container
docker cp mycontainer:/var/log/app.log ./app.log
Use Case: Retrieve logs post-run.
🗃️ Database & Stats
28. Run MySQL with Volume
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=root -v mysql-data:/var/lib/mysql mysql:8
Use Case: Persistent DB for dev environments.
29. MySQL CLI Inside Container
docker exec -it mysql-db mysql -uroot -proot
Use Case: Access MySQL shell inside the container.
30. One-Time Resource Usage
docker stats --no-stream
Use Case: Snapshot of resource usage per container.
🧠 Conclusion
Docker isn't just about running containers — it's about mastering how to build, manage, and optimize them in real scenarios. Bookmark this list or keep it handy during troubleshooting and CI/CD pipeline builds!
Top comments (0)