Sub topic 6
Reproducing a Container That Exits at Startup
This chapter assumes you can run docker run and docker logs, you know the difference between ENTRYPOINT and CMD, and you can read an exit code. If the container dies in under a second, you often cannot docker exec into it, because there is no running process to attach to. The fix is to start a new container from the same image with a shell in place of the normal startup command.
Why you can't exec into it
docker exec runs a command inside a running container. A container that has already exited has no running process, so docker exec fails with an error saying the container is not running. A container in a restart loop is running for only a moment at a time, which makes exec unreliable too.
Instead, you create a throwaway container from the same image and replace the entrypoint.
docker run --rm -it --entrypoint sh myimage:latest--rmdeletes the container when you leave the shell.-itgives you an interactive terminal.--entrypoint shreplaces the image'sENTRYPOINTwith a shell. Whatever follows the image name becomes the arguments to that entrypoint.
Before you run it, check the facts you need from the failed container with docker inspect (see the earlier chapters): its exit code, its Config.Entrypoint and Config.Cmd, and its environment.
What to check inside the shell
Once you have a prompt, run the failing command by hand and watch what happens.
- Look at the files:
ls -l /app. Is the binary or script where the entrypoint expects it? - Check permissions: is the file executable? A missing execute bit gives exit code 126.
- Check the environment:
env. Are the variables the app needs set? - Run the original command yourself, for example
/app/start.sh. The error now prints in front of you, and the shell stays open after it fails. - Check the interpreter: a script with
#!/bin/bashon an image without bash exits with 127, and the error message is misleading, because the script file does exist.
Matching the original conditions
A fresh container only reproduces the failure if it matches the original one. Pass the same settings you used before.
docker run --rm -it \
--env-file ./app.env \
-v "$PWD/config:/app/config:ro" \
--network mynet \
--entrypoint sh myimage:latestDifferences in environment variables, mounts, user, or network are common reasons a failure "doesn't reproduce". Compare with docker inspect web and copy the relevant options.
Running the command under a debugger-friendly shell
If the entrypoint is a shell script, run it with tracing turned on, so each line prints before it executes.
docker run --rm -it --entrypoint sh myimage:latest -c 'sh -x /app/start.sh'The -x flag shows which line failed and with what values.
The overall flow
Common mistakes
- Putting arguments before the image name. Options go before the image. Everything after the image name is passed to the entrypoint.
- Forgetting to set
--entrypointand passingshafter the image name. With an entrypoint defined, thatshbecomes an argument to the entrypoint and does not replace it. - Debugging a different image. If
latesthas been rebuilt since the failure, use the image ID fromdocker inspect --format '{{.Image}}' web.
Exercise
A container built from myimage:latest exits immediately with code 127. Write the command that gives you a shell in a throwaway container from that image, and name two things you would check first.
Show answer
docker run --rm -it --entrypoint sh myimage:latest. Check that the file named in the entrypoint exists at that path, and that its interpreter (the #! line) exists in the image. Exit code 127 means "command not found". You could also check that PATH includes the directory of the command.