Debug a container: logs, exec, inspect, port clashes and exit codes
Find out why a container stops at once or will not start, using logs, exec, inspect and the exit code, and fix a port clash.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: -up. The doctor reads the chart first, then listens, then looks up the file of details. If the patient has already left, only the chart and the file remain. A different problem is the door: two people cannot use the same door number at once.
The chart is the log. Listening is exec, and it only works while the patient is still there. The file is inspect. The door number is the host port.
Tom, a tester at Daily List, a to-do list app, finds a fault in a container that exits at once. Lily watches the with him.
What it is: This is a slice of Docker, the program that runs containers. A is the number left when the container stops, and a means your port is already taken. Exec into a running container is docker exec, and it works only while the main program is still running. Restarting with no new fact is the neighbour. It feels busy, and it does not explain the failure.
Why it matters: A broken Daily List test container wastes a whole (CI) run if Tom only restarts it. The log, the exit code and inspect turn "it will not work" into a cause he can say in the stand-up. A port clash is a one-line fix once he sees which number is taken.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB A doctor reading a chart and examining a patient.
- Chart
- Listen
- Details
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The chart notes | The container logs |
| The patient already gone home | A stopped container |
| Listening to the patient in the room | Exec into a running container |
| The record of the patient details | The inspect report |
WHERE THIS PICTURE STOPS
- A chart is written by nurses, but logs are written by the program itself.
- A patient in the room can answer, and a stopped container cannot.
- A check-up is one visit, but a bug may need more than one run.
HOOK
Chart first, then the patient.
TELL A FRIEND
Read the chart, listen, then look at the details.
OFTEN MIXED UP WITH Docker Compose: service names, ports, a named volume and exec. That topic uses compose, networks, volumes and exec when things work. This one finds the cause when a container will not start or keeps stopping.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
docker container logs (Docker Docs, CLI reference) ↗ — Docker · Docker docs for container logs: logs fetches the output a container wrote, with options such as follow and tail
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Running containers (Docker documentation) ↗ — Docker · Docker docs on running containers: 125 means Docker failed, 126 means the command could not be run, 127 means it was not found, and other codes come from the command
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Container exec and inspect (Docker documentation) ↗ — Docker · Docker docs on exec and inspect: exec runs only while the main process runs, and inspect prints container details
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Publishing and exposing ports (Docker documentation) ↗ — Docker · Docker guide on publishing ports: the host port comes first, and leaving it out lets Docker choose a free one
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.