Skillversitas

Docker Tips and Tricks

Tips and tricks for higher Docker productivity.

1. Aliases

# Add to ~/.bashrc or ~/.zshrc
alias dc='docker-compose'
alias dps='docker ps'
alias dpsa='docker ps -a'
alias dex='docker exec -it'
alias dlog='docker logs -f'
alias dprune='docker system prune -f'
alias dimages='docker images'
alias dvol='docker volume ls'
alias dnet='docker network ls'

# Usage:
dc up -d
dps
dex web bash
dlog api
dprune

2. Docker Clean

# Delete all unused
docker system prune -a --volumes -f

# Delete images only
docker image prune -a -f

# Delete volumes only
docker volume prune -f

# Delete networks only
docker network prune -f

3. Quick Commands

# Run a command in all containers
docker ps -q | xargs -I {} docker exec {} date

# Stop all containers
docker stop $(docker ps -q)

# Delete all containers
docker rm -f $(docker ps -aq)

# View all running containers
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

4. Docker Context

# View contexts
docker context ls

# Switch context
docker context use remote-server

# Run a command remotely
docker -H tcp://192.168.1.100:2375 ps

5. Docker Init

# Generate a Dockerfile
docker init

# Choose a language: Node, Python, Go, Rust, or Other

Use Case: Daily Tips

# Morning: Check the status
docker-compose ps

# Afternoon: Debug
docker-compose logs -f api

# Evening: Cleanup
docker system prune -f

# Night: Back up
./backup.sh

6. Docker Scan

# Scan an image
docker scout cves myapp:latest

# Scan critical only
docker scout cves --only-severity critical myapp:latest

7. Docker Stats

# Real-time stats
docker stats

# Export stats
docker stats --no-stream --format \
  "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

8. Docker Events

# View events
docker events

# Filter events
docker events --filter 'type=container'
docker events --filter 'type=image'

9. Docker Export/Import

# Export a container
docker export container_name > container.tar

# Import a container
docker import container.tar new_image:tag

# Save/Load an image
docker save image_name > image.tar
docker load < image.tar

10. Docker Debug

# Enter a crashed container
docker run -it --rm image_name sh

# View the environment
docker inspect container_name | grep -A 5 "Env"

# View mounts
docker inspect container_name | grep -A 10 "Mounts"

Use Case: Troubleshooting

# Problem: Container crashes
docker logs container_name
docker inspect container_name

# Problem: Port conflict
docker port container_name
lsof -i :80

# Problem: Volume permissions
docker exec container_name ls -la /path
docker exec container_name chown -R node:node /path

Best Practices

  1. Use .dockerignore: For faster builds
  2. Multi-stage build: For a smaller image
  3. Non-root user: For security
  4. Health check: For monitoring
  5. Resource limits: For every container
  6. Named volumes: For persistent data
  7. Custom network: For DNS resolution
  8. Secrets management: For sensitive data
  9. Back up regularly: For data safety
  10. Monitor: Use docker stats and logs

Conclusion

Docker tips and tricks increase productivity. Use aliases, cleanup, and debugging tools for more effective Docker management.