Sub topic 15
Troubleshooting containers that fail to run
This chapter assumes you can start containers with docker run, and that you know docker ps, docker logs, and docker exec from earlier chapters. It gives you a repeatable way to find out why a container will not start or keeps stopping.
Start with the state
A container that fails usually ends up in the Exited state, Restarting, or never appears at all. Check which one before you guess at a cause.
docker ps -a
docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' webdocker ps without -a hides stopped containers, which is why a failed container seems to vanish. The -a flag shows it, along with its exit code.
Read the exit code
The exit code tells you how the main process ended.
| Exit code | Usual meaning |
|---|---|
| 0 | The process finished normally. The container had nothing left to do. |
| 1 | The application reported an error. Read the logs. |
| 125 | Docker itself failed to run the command, for example a bad flag. |
| 126 | The command was found but cannot be executed (permissions). |
| 127 | The command was not found inside the image. |
| 137 | The process was killed (SIGKILL), often by the out-of-memory killer. |
| 143 | The process received SIGTERM, for example from `docker stop`. |
A container runs only as long as its main process. An exit code of 0 right after start often means the image's command is a short task, or that you ran a shell without -it.
Read the logs
docker logs --tail 50 web
docker logs -f web--tail 50 shows the last 50 lines. -f follows new output. For a container in a restart loop, logs from earlier attempts remain available, so you can read why the last one died.
Common causes
- Port already in use.
docker run -p 8080:80fails with "port is already allocated" if another process or container holds host port 8080. Rundocker psto find the other container, or choose a different host port. - Missing configuration. The
postgresimage exits immediately whenPOSTGRES_PASSWORDis not set. The logs name the missing variable. - Out of memory. With
--memory 256m, a process that needs more is killed.OOMKilled=trueindocker inspectconfirms it. Raise the limit or reduce what the application uses. - Name conflict. Reusing
--name webfails while an old container with that name exists, even a stopped one. Remove it withdocker rm web. - Wrong command. Exit code 127 or 126 means the program is missing or not executable in the image.
- Volume permissions. A bind-mounted directory the container user cannot write to causes startup errors in the logs.
Debug a container that exits at once
You cannot use docker exec on a stopped container. Start a new container from the same image with a shell instead of the default command.
docker run --rm -it --entrypoint sh nginx:1.27--entrypoint sh replaces the image's startup command, so you can look at files, run the real command by hand, and see its errors directly.
When the container is up but unreachable
If the status is Up yet requests fail, check these in order:
- Run
docker port webto confirm the port mapping exists. - Run
docker exec -it web shand request the service from inside the container. If that works, the problem is the mapping or the host firewall. - Confirm the application listens on
0.0.0.0and not only127.0.0.1. A process bound to loopback inside the container cannot be reached through a published port. - For container-to-container traffic, confirm both containers are on the same user-defined network and use the container name as the host.
Exercise
A container started with docker run -d --name api --memory 128m myapp shows Exited (137) and OOMKilled=true. What happened, and what are two ways to fix it?
Show answer
The application used more than 128 MB, so the kernel's out-of-memory killer ended it with SIGKILL, which gives exit code 137. Either raise the limit (for example --memory 512m) or reduce the application's memory use.
Exercise
docker run -d --name web -p 8080:80 nginx:1.27 fails with "port is already allocated". Which command helps you find the cause, and what can you change to start the container?
Show answer
Run docker ps to see whether another container publishes host port 8080 (or check for a host process using it). Stop that container, or publish a different host port such as -p 8081:80.