📋 Logging & Debugging
Container Log Basics
bash
# View container logs
docker logs myapp
# Follow logs in real time (like tail -f)
docker logs -f myapp
# Show only the last 100 lines
docker logs --tail 100 myapp
# Show only logs from the last hour
docker logs --since 1h myapp
# With timestamps
docker logs -t myapp
# Combined: follow + timestamps + last 50 lines
docker logs -f -t --tail 50 myapp
# Where are logs stored?
docker inspect --format='{{.LogPath}}' myapp
/var/lib/docker/containers/abc123.../abc123...-json.logLog Driver Configuration
Docker uses the json-file driver by default, storing logs in JSON format on the host disk. Without log rotation configured, logs will grow indefinitely and eventually fill up the disk!
bash
# Global log rotation configuration (/etc/docker/daemon.json)
cat > /etc/docker/daemon.json << 'EOF'
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
EOF
systemctl restart docker
# Per-container configuration
docker run -d --log-opt max-size=10m --log-opt max-file=3 myapp
# Common log drivers
# json-file — default, JSON format, supports docker logs
# syslog — sends to syslog, suitable for existing centralized logging
# journald — sends to systemd journal
# fluentd — sends to Fluentd
# none — disables logging (not recommended)
# Clean up logs from stopped containers (free disk space)
docker system prune -f
# Check Docker disk usage
docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 12 5 2.8GB 1.5GB (53%)
Containers 8 5 120MB 35MB (29%)
Local Volumes 15 8 1.2GB 400MB (33%)
Build Cache 0 0 0B 0BDebugging Tips
bash
# Container exits on startup? Check the exit code
docker inspect --format='{{.State.ExitCode}}' myapp
1
# Exit code meanings: 0=normal, 1=app error, 137=OOM/killed, 139=segfault, 143=SIGTERM
# Enter a crashing container for debugging
docker run -it --entrypoint /bin/sh myapp
# View a container's environment variables
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' myapp
# View a container's mount points
docker inspect --format='{{json .Mounts}}' myapp | python3 -m json.tool
# View container networking
docker inspect --format='{{json .NetworkSettings.Networks}}' myapp | python3 -m json.tool
# Copy files out of a container
docker cp myapp:/app/config.yml ./config.yml
# Real-time resource monitoring for all containers
docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
a1b2c3d4e5f6 nginx 0.50% 12.3MiB / 512MiB 2.40% 1.2MB / 800kB 0B / 0B
f6e5d4c3b2a1 mysql 45.20% 1.2GiB / 2GiB 58.59% 5.6MB / 3.2MB 12MB / 8MB