The Docker logs are the output generated by the container’s processes that Docker captures so we can consult them later.
We’ve learned to launch containers in Detached mode (-d). This is great because it frees up our terminal, but it has a dangerous side effect: we stop seeing what’s happening.
- If you launch a database and it fails to start, you won’t see the error.
- If your web server crashes upon receiving a request, you won’t know.
In this article, we’re going to learn how to listen to what our containers are saying and monitor their health.
Docker Logs
In traditional systems, when an application failed, you had to go looking for a lost file in /var/log/nginx/error.log or similar.
In Docker, the philosophy is different. Docker automatically captures everything the main process writes to Standard Output (STDOUT) and Standard Error (STDERR).
To see what a container has said, we use docker logs:
docker logs <container_name>
This will dump the entire history of what has happened since the container started onto your screen.
If you are programming an app for Docker (in Node, Python, C#…), do not write logs to text files inside the container.
Simply write to the console (console.log, print). Docker will take care of capturing, rotating, and managing it.
Resource Monitoring (docker stats)
Sometimes the problem isn’t a crash, but that it’s slow. Or worse, your whole computer has been slow since you launched those containers. How do we know who is eating up the CPU or RAM?
Docker includes a very powerful native “Task Manager”:
docker stats
You’ll see a real-time table that updates every second:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O
a1b2c3d4 my-db 0.15% 450MiB / 8GiB 5.20% 1.2kB / 0B
e5f6g7h8 my-web 0.02% 22MiB / 8GiB 0.12% 500B / 0B
Here you can quickly spot the culprit:
- CPU %: If a container is at 100%, something got stuck in an infinite loop.
- MEM USAGE: Here you’ll see if that database or Java server is eating up all your available RAM.
Inspecting processes (docker top)
If you want to dig deeper and see what specific processes are running inside a container (without entering it), you can use:
docker top my-container
This is useful to verify, for example, if your web server has correctly launched its worker threads or if there are zombie processes.
