Docker Container Management: Essential Commands and Best Practices¶
Docker provides powerful command-line tools for managing containers, images, and system resources. Understanding these commands is crucial for efficient container orchestration and system maintenance. This guide covers essential commands for daily Docker operations and maintenance tasks.
Container Lifecycle Management¶
Basic Container Operations¶
View and manage running containers:
# List only running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Show only container IDs (useful for scripting)
docker ps -q
# Custom format output
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
Container Control¶
Manage container states:
# Start one or more containers
docker start container_name [container2_name ...]
# Stop containers gracefully (SIGTERM)
docker stop container_name
# Force stop containers (SIGKILL)
docker kill container_name
# Restart containers
docker restart container_name
# Pause/unpause container processes
docker pause container_name
docker unpause container_name
Resource Monitoring and Inspection¶
Container Logs¶
Access container logs for troubleshooting:
# View container logs
docker logs container_name
# Follow log output in real-time
docker logs -f container_name
# Show last n lines
docker logs --tail 100 container_name
# Show logs with timestamps
docker logs -t container_name
Resource Usage¶
Monitor container performance:
# Live resource usage statistics
docker stats
# Process list in specific container
docker top container_name
# Detailed container information
docker inspect container_name
Image Management¶
Basic Image Operations¶
Manage Docker images:
# List all images
docker images
# Remove specific image
docker rmi image_name:tag
# Remove dangling images
docker image prune
# Remove all unused images
docker image prune -a
System Maintenance¶
Cleanup Operations¶
Maintain system health:
# Remove all stopped containers, unused networks, dangling images
docker system prune
# Include unused images in cleanup
docker image prune -a
# Remove unused volumes
docker system prune --volumes
# Remove specific container
docker rm container_name
Volume Management¶
Handle persistent storage:
# List all volumes
docker volume ls
# Remove unused volumes
docker volume prune
# Create named volume
docker volume create volume_name
# Inspect volume details
docker volume inspect volume_name
Advanced Operations¶
Container Execution¶
Interact with running containers:
# Interactive terminal
docker exec -it container_name /bin/bash
# Run single command
docker exec container_name command
# Copy files to/from container
docker cp local_file container_name:/path
docker cp container_name:/path local_file
Batch Operations¶
Manage multiple containers:
# Stop all running containers
docker stop $(docker ps -q)
# Remove all stopped containers
docker rm $(docker ps -a -q)
# Remove all containers (force)
docker rm -f $(docker ps -a -q)
Additional Tips¶
- Resource Management
# Limit container memory docker run --memory="512m" image_name # Limit CPU usage docker run --cpus="1.5" image_name
- Network Operations
# List networks docker network ls # Create network docker network create network_name
Best Practices¶
- Always use meaningful container names with
--name
flag - Implement regular cleanup schedules using
docker system prune
- Use
--rm
flag for temporary containers - Monitor resource usage with
docker stats
before cleanup operations - Implement logging rotation to prevent disk space issues
- Use container health checks for production deployments
- Always tag images with specific versions instead of using
latest
- Create a
.dockerignore
file to exclude unnecessary files from builds
This comprehensive set of commands and practices will help maintain a healthy Docker environment and efficient container operations. Regular system maintenance using these commands ensures optimal resource utilization and system performance.