Docker Commands
Essential Docker commands for containers, images, and orchestration
10 min read
dockercontainersdevopsdeploymentvirtualization
Table of Contents
Container Management
Running Containers
Start and run Docker containers with various options
bash
💡 Use -d for background containers
⚠️ Always name containers for easier management
📌 Use --rm for temporary containers
✅ Set resource limits in production
Container State Management
Control container lifecycle - start, stop, restart, and remove
bash
💡 docker ps -a shows stopped containers too
⚠️ Use -f flag carefully to force remove
📌 Container prune removes all stopped containers
✅ Always stop before removing unless using -f
Container Inspection & Logs
Debug and inspect running containers for troubleshooting
bash
💡 Use -f to follow logs in real-time
📌 docker exec -it for interactive sessions
⚡ Inspect with --format for specific fields
✅ Copy files without stopping container
Image Management
Building Images
Build Docker images from Dockerfiles with various options
bash
💡 Use .dockerignore to exclude files
⚡ BuildKit provides advanced features
📌 Multi-stage builds reduce image size
✅ Always tag images with versions
Image Operations
Manage Docker images - pull, push, tag, and remove
bash
💡 Use specific tags instead of latest
⚠️ Prune regularly to save disk space
📌 Save images for offline transfer
✅ Always login before pushing
Image Registry
Work with Docker registries and manage image distribution
bash
🔒 Always use HTTPS for production registries
💡 Tag images with registry URL
📌 Local registry useful for testing
✅ Implement authentication for private registries
Volumes & Networks
Volume Management
Create and manage Docker volumes for persistent data storage
bash
💡 Named volumes persist data between containers
📌 Bind mounts for development, volumes for production
⚠️ Prune removes all unused volumes
✅ Backup important volumes regularly
Network Management
Configure Docker networks for container communication
bash
💡 Containers on same network can communicate by name
🔒 Use internal networks for security
📌 Bridge is default, overlay for Swarm
✅ Custom networks provide better isolation
Docker Compose
Compose Basics
Essential Docker Compose commands for multi-container applications
bash
💡 Use -d for background operation
⚡ --scale for horizontal scaling
📌 -f to specify custom compose files
✅ Always use down -v to clean volumes
Compose Configuration
Docker Compose file configuration and advanced features
yaml
💡 Use depends_on for service dependencies
📌 Version 3.8 is recommended for most uses
⚡ Use extension fields to avoid repetition
✅ Always set resource limits in production
System & Cleanup
System Management
Monitor and manage Docker system resources and information
bash
💡 Regular pruning prevents disk issues
⚠️ Prune -a removes all unused images
📌 Use filters to control what gets pruned
✅ Monitor disk usage with system df
Debugging & Troubleshooting
Debug containers and resolve common Docker issues
bash
💡 nicolaka/netshoot has many network tools
🔍 Use --progress=plain for detailed build output
📌 Override entrypoint to debug failed containers
✅ Always check logs first when debugging