SyntaxStudy
Sign Up
Docker Container Logs and Debugging
Docker Beginner 1 min read

Container Logs and Debugging

Observing what is happening inside a container is critical for debugging. docker logs streams the container's stdout and stderr. The --follow flag tails the log in real time; --since and --until filter by timestamp; --tail N limits output to the last N lines. For production, configure a logging driver (json-file, syslog, AWS CloudWatch) rather than relying on docker logs. docker exec -it container sh opens an interactive shell inside a running container without stopping it. This is invaluable for inspecting the container filesystem, checking environment variables with env, testing network connectivity, or examining process state. For containers based on scratch or distroless images with no shell, use docker cp to copy files out for inspection. docker inspect returns a JSON document containing the container's configuration, network settings, mount points, environment variables, and state. Piping through jq enables targeted queries. The docker events command streams real-time events from the Docker daemon.
Example
# Tail logs of a named container
docker logs api --tail 50 --follow

# Filter logs by time
docker logs api --since 1h
docker logs api --since "2025-01-15T10:00:00"

# Open a shell in a running container
docker exec -it api sh
docker exec -it api bash

# Run a single command inside the container
docker exec api env
docker exec api cat /etc/hosts
docker exec api ps aux

# Check container resource usage
docker stats api --no-stream

# Inspect network and mounts
docker inspect api \
  --format '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{"\n"}}{{end}}'

# Stream Docker daemon events
docker events --filter container=api

# Debug a crashed container by overriding entrypoint
docker run --rm -it --entrypoint sh myapp:2.1.0