Docker
Practical Docker commands for container lifecycle management, image building, log inspection, resource cleanup, and networking.
docker compose (e.g., docker compose up -d).Container Quick Reference
Most-used Docker container commands for listing, starting, stopping, and removing containers.
# List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a running container docker stop <container_id_or_name> # Remove a stopped container docker rm <container_id_or_name> # Force remove a running container docker rm -f <container_id_or_name>
Build & Tag an Image
Build a Docker image from a Dockerfile in the current directory and tag it with a name and version. Use '.' to build from current directory.
# Build with tag docker build -t myapp:1.0.0 . # Build with build args docker build \ --build-arg NODE_ENV=production \ --build-arg APP_PORT=3000 \ -t myapp:latest . # List local images docker images
Run Container with Options
Start a container with common options: detached mode, port mapping, environment variables, and automatic restart policy.
docker run -d \ --name myapp \ --restart unless-stopped \ -p 3000:3000 \ -e NODE_ENV=production \ -e DATABASE_URL=postgres://user:pass@db:5432/mydb \ -v /host/data:/app/data \ myapp:latest
Container Logs
View and follow container logs. Use --since to limit to recent logs, and --tail to cap output. Useful for debugging production issues.
# Follow live logs docker logs -f myapp # Last 100 lines docker logs --tail 100 myapp # Logs from last 1 hour docker logs --since 1h myapp # Logs between timestamps docker logs --since "2025-01-01T10:00:00" --until "2025-01-01T11:00:00" myapp
Execute Command Inside Container
Open an interactive shell inside a running container. Use sh if bash is not available (Alpine-based images).
# Open bash shell docker exec -it myapp bash # Open sh (for Alpine/minimal images) docker exec -it myapp sh # Run a single command docker exec myapp cat /etc/os-release # Run as root (useful for debugging) docker exec -it -u root myapp bash
System Cleanup
Free up disk space by removing stopped containers, dangling images, unused networks, and build cache. Use with caution in production.
# Remove all stopped containers docker container prune -f # Remove dangling images (untagged) docker image prune -f # Remove all unused images (not just dangling) docker image prune -a -f # Nuclear option: remove everything unused docker system prune -a --volumes -f # Check disk usage first docker system df
Network & Volume Management
Manage Docker networks and volumes. Useful when containers need to communicate or share persistent data.
# List networks
docker network ls
# Create a custom bridge network
docker network create mynetwork
# Inspect container's network settings
docker inspect --format='{{json .NetworkSettings}}' myapp | jq
# List volumes
docker volume ls
# Remove unused volumes
docker volume prune -f
# Inspect a volume mount path
docker volume inspect myvolumeContainer Resource Usage
Monitor real-time CPU, memory, network, and block I/O usage for running containers. Useful for performance debugging.
# Live stats for all containers
docker stats
# Stats for a specific container (no-stream = snapshot)
docker stats --no-stream myapp
# Inspect container config (ports, env, mounts, etc.)
docker inspect myapp
# Check container exit code and reason
docker inspect myapp --format='{{.State.ExitCode}} {{.State.Error}}'