Sub topic 7
Reading logs and inspecting a running container
This chapter assumes you can build and run the myapp:1.0 image from the previous chapters. It also assumes you know what a published port is. Here you will learn how to find out what a container is doing and why it fails.
Where container logs come from
Docker captures everything your process writes to standard output (stdout) and standard error (stderr). docker logs reads that captured stream. This is why a Node.js app should log with console.log and not write to a file inside the container. A log file inside the container is invisible to docker logs, and it disappears when the container is removed.
Start the container from the earlier chapter:
docker run --rm -d -p 3000:3000 --name myapp myapp:1.0Now read its output:
docker logs myapp
docker logs --tail 20 myapp
docker logs --since 5m myapp
docker logs -f myapp--tail 20shows only the last 20 lines.--since 5mshows lines from the last five minutes.-ffollows the stream liketail -f. Press Ctrl+C to stop following. This does not stop the container.
Inspecting state with docker ps and docker inspect
docker ps lists running containers. Add -a to include stopped ones. A container that crashed on startup appears only with -a.
docker inspect prints a large JSON document describing one container. The --format option extracts a single field using a Go template:
docker inspect --format '{{.State.Status}}' myapp
docker inspect --format '{{.State.ExitCode}}' myapp
docker inspect --format '{{json .Config.Env}}' myapp
docker inspect --format '{{json .NetworkSettings.Ports}}' myappThese show the status, the exit code of the main process, the environment variables, and the port mappings. Use them to check what the container really received, which may differ from what you meant to pass.
Looking inside the container
docker exec runs a second process inside a running container. Use it for an interactive shell:
docker exec -it myapp shThe node:22-alpine image has sh but not bash. Once inside, you can check files and test the app from its own point of view:
whoami
ls /app
wget -qO- http://localhost:3000
exitwhoami should print node, which confirms the USER node instruction worked. Two other commands give a quick view from the host:
docker top myapp
docker stats --no-stream myappdocker top lists the processes in the container. docker stats shows CPU and memory use.
A debugging routine
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `docker logs` says no such container | The container was started with `--rm` and has already exited | Rerun without `--rm` while debugging, then read the logs |
| `docker exec` says the container is not running | The main process crashed | Use `docker ps -a`, then `docker logs` |
| `exec: "bash": executable file not found` | Alpine images have no bash | Use `sh` |
| Logs are empty but the app runs | The app writes to a file instead of stdout | Log to stdout and stderr |
| Works inside the container, fails from the host | The app listens on `127.0.0.1` | Listen on `0.0.0.0` |
Stop the container when you finish:
docker stop myappExercise
A container started with docker run -d --name api myapp:1.0 exits a few seconds after start. List the commands, in order, that you would use to find the exit code and the last 30 lines of its output.
Show answer
Run docker ps -a to confirm the container exists and has exited. Run docker inspect --format '{{.State.ExitCode}}' api to get the exit code. Run docker logs --tail 30 api to see the last 30 lines. docker exec would not work here because the container is not running.