Docker has become the standard for application packaging and deployment. Whether you are a developer building microservices or a system administrator managing production containers, mastering these 10 Docker commands will significantly boost your productivity.
1. docker compose up --build --force-recreate
When developing with Docker Compose, you often need to rebuild images and recreate containers after code changes. This single command handles both:
# Rebuild images and recreate all containers
docker compose up --build --force-recreate -d
# Only rebuild specific service
docker compose up --build --force-recreate -d webapp
The --force-recreate flag ensures containers are recreated even if configuration hasn't changed, which is essential when debugging environment-related issues.
2. docker exec -it [container] /bin/sh
Access a running container's shell for debugging:
# Access container with bash
docker exec -it my-webapp /bin/bash
# For Alpine-based images (no bash)
docker exec -it my-webapp /bin/sh
# Run a specific command without interactive shell
docker exec my-webapp cat /etc/nginx/nginx.conf
3. docker logs --follow --tail 100
Tail container logs in real-time, showing the last 100 lines:
# Follow logs with timestamp
docker logs --follow --tail 100 --timestamps my-webapp
# Filter logs by time range
docker logs --since "2026-03-01T08:00:00" my-webapp
# Combine with grep for filtering
docker logs my-webapp 2>&1 | grep -i error
4. docker system df
Check Docker disk usage to identify space-consuming resources:
# Show disk usage summary
docker system df
# Verbose output with individual items
docker system df -v
This is invaluable when your server runs low on disk space. Follow up with docker system prune to reclaim space.
5. docker inspect --format
Extract specific information from containers using Go templates:
# Get container IP address
docker inspect --format "{{.NetworkSettings.IPAddress}}" my-webapp
# Get all environment variables
docker inspect --format "{{.Config.Env}}" my-webapp
# Get mounted volumes
docker inspect --format "{{.Mounts}}" my-webapp
# Get container health status
docker inspect --format "{{.State.Health.Status}}" my-webapp
6. docker stats --no-stream
Get a snapshot of resource usage across all running containers:
# One-time snapshot of all containers
docker stats --no-stream
# Format output for scripting
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
7. docker network inspect
Debug networking issues between containers:
# List all networks
docker network ls
# Inspect a specific network
docker network inspect bridge
# Find which containers are on a network
docker network inspect my-network --format "{{range .Containers}}{{.Name}} {{end}}"
8. docker cp
Copy files between host and container without rebuilding:
# Copy from container to host
docker cp my-webapp:/var/log/nginx/error.log ./error.log
# Copy from host to container
docker cp ./config.json my-webapp:/app/config.json
9. docker build --target
Use multi-stage build targets for different environments:
# Build only the development stage
docker build --target development -t myapp:dev .
# Build the production stage
docker build --target production -t myapp:prod .
# Build with build arguments
docker build --build-arg NODE_ENV=production -t myapp:prod .
10. docker compose config
Validate and view the resolved Docker Compose configuration:
# Validate and display merged config
docker compose config
# Check for errors without output
docker compose config --quiet
# Show only service names
docker compose config --services
Bonus: Clean Up Everything
# Remove all stopped containers, unused networks, dangling images
docker system prune -a --volumes
# Remove specific unused images
docker image prune -a --filter "until=720h"
Master these commands and your daily Docker workflow will become significantly faster and more efficient.
Dive Deeper
- Docker Fundamentals — Complete guide to Docker from basics to production
- Docker Compose & Multi-Container Applications — Master multi-service deployments