This article covers Docker Logs and Troubleshooting in detail. Your container exited two seconds after starting, the web app returns 502, or everything worked yesterday and nothing works today. In all of these cases the answer is in the same place: the logs. Mastering docker logs and a handful of inspection commands turns frustrating guesswork into a five-minute diagnosis. This guide shows you how to read logs effectively, follow them live, control their size, and fix the container errors you will meet most often in production. It assumes Docker is already installed — otherwise start with our Ubuntu 24.04 installation guide.
Reading Docker Logs – Docker Logs and Troubleshooting
Docker captures everything a container writes to standard output and standard error. View it with:
# All logs of a container
docker logs web
# Follow logs in real time (Ctrl+C to stop)
docker logs -f web
# Only the last 100 lines, with timestamps
docker logs --tail 100 -t web
# Logs from the last 10 minutes only
docker logs --since 10m web
The --since and --until flags accept durations (30s, 10m, 2h) or timestamps, which is invaluable on busy systems where a container produces megabytes of output per hour. For Compose projects, docker compose logs -f web does the same per service and prefixes each line with the service name.

When There Are No Logs: Inspect the Container – Docker Logs and Troubleshooting
Sometimes the container dies before it can log anything useful. Then inspect its state:
# Why did it stop? Check State.ExitCode and State.Error
docker inspect web
# Show the last command and status summary
docker ps -a
# Print just the exit code
docker inspect -f '{{.State.ExitCode}}' web
Exit codes tell a story: 0 means the main process finished normally (a common surprise — many “crashes” are apps that simply completed), 1 is a generic application error, 126 means the command could not be executed, 127 means the command was not found, and 137 means the container was killed by SIGKILL — almost always the Linux OOM killer or a manual docker kill.
Fixing the Most Common Container Errors
“Port is already allocated”. Another process — often an older container — already uses that host port. Find it with docker ps or ss -tlnp, then stop it or choose a different host port in the -p mapping.
“Permission denied” on mounted files. The process inside the container runs as a different UID than the owner of your bind-mounted files. Align the UID, run the container with --user $(id -u):$(id -g) for development images, or fix ownership on the host directory.
Container keeps restarting. Check docker logs right after a restart and look at the restart policy with docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' web. A crash loop usually means a missing environment variable, an unreachable database, or bad configuration — all visible in the first log lines.
OOM killed (exit code 137). Confirm with dmesg | grep -i "out of memory" on the host. Either give the container more memory, set a sane limit with --memory so one container cannot starve the host, or fix the application’s memory usage.
DNS resolution failures inside containers. Test with docker exec web getent hosts example.com. If it fails on custom networks, check the host’s DNS configuration — Docker inherits resolvers from the host, and some VPN or systemd-resolved setups need explicit --dns flags.
Debugging a Running Container
# Open a shell (if the image has one)
docker exec -it web sh
# Live resource usage
docker stats
# Changes made to the container filesystem
docker diff web
# Events stream: starts, stops, health changes
docker events
Slim and distroless images often have no shell at all. In that case, rely on logs and inspect output, or debug from a sidecar container sharing the network namespace: docker run --rm -it --network container:web nicolaka/netshoot gives you a full network toolbox pointed at the target container.
Controlling Log Size with Log Rotation
By default, the json-file logging driver grows without limit — a chatty container can fill a disk and take the whole server down. Configure rotation per container:
docker run -d \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpine
Or set it globally in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" }
}
Restart the Docker daemon after changing the file. Existing containers keep their old settings; recreate them to apply the new limits.
Make Your Logs Worth Reading
Log quality is decided by the application, not by Docker. Log to stdout and stderr — never to files inside the container, which are invisible to docker logs. Include timestamps and severity levels, prefer structured JSON output for anything you plan to ship to a log platform, and keep health-check and access noise at debug level. A container whose logs clearly state what it was doing when it failed is a container you can debug at 3 a.m. without heroics.
Conclusion
Logs first, inspect second, guess never. With docker logs, exit codes, docker inspect and docker stats you can diagnose the overwhelming majority of container problems in minutes — and with log rotation configured, your disks survive your chattiest services. For the complete flag reference, see the official docker logs documentation.