Sub topic 6
Reproducing a Container That Exits at Startup
This chapter assumes you can read docker ps and docker inspect output, know how ENTRYPOINT and CMD combine, and are comfortable in a Linux shell. It also assumes you have already looked at the exit code and logs of the failed container.
Why You Need to Reproduce the Failure
A container that exits within a second of starting gives you nothing to attach to. docker exec only works on a running container, and the logs may be empty or cut off. The usual fix is to start a new container from the same image, but run a shell instead of the normal startup command. The shell stays alive, so you can run the real command by hand and watch it fail.
Reproduction is useful when:
- The logs are empty, which often means the process never started (exit code 126 or 127).
- The logs show an error but not its cause, such as "file not found" for a path you believe exists.
- The failure depends on the image contents, such as a missing library, a wrong line ending in a script, or a wrong permission bit.
How ENTRYPOINT and CMD Decide What Runs
Docker builds the startup command by joining the entrypoint and the command. If the image defines both, the command is appended to the entrypoint as arguments.
| Image defines | Container runs |
|---|---|
| `CMD ["node", "server.js"]` only | `node server.js` |
| `ENTRYPOINT ["/entrypoint.sh"]` only | `/entrypoint.sh` |
| Both | `/entrypoint.sh node server.js` |
This matters because arguments you put after the image name in docker run replace CMD but do not replace ENTRYPOINT. Running docker run myimage:latest sh against an image with an entrypoint script runs /entrypoint.sh sh. The script may fail before your shell ever starts. To bypass the script, you must override the entrypoint itself.
Step 1: Read What the Image Will Run
Before you override anything, record the original command so you can run it by hand later.
docker inspect --format 'entrypoint={{json .Config.Entrypoint}} cmd={{json .Config.Cmd}} workdir={{.Config.WorkingDir}} user={{.Config.User}}' webYou can run the same command against the image name if the container has been removed. Use docker image inspect in that case. Note the working directory and user, because both affect whether the command works.
Step 2: Start a Shell Instead
Use --entrypoint to replace the entrypoint. Anything after the image name becomes the new command.
docker run --rm -it --entrypoint sh myimage:latestThe --rm flag deletes the container when you leave, so debugging sessions do not pile up as stopped containers. The -it flags give you an interactive terminal. If the image has bash, use --entrypoint bash.
Note that the shell name is the entrypoint and nothing else. Do not write --entrypoint "sh -c ...", because Docker treats the whole string as one executable name. To pass arguments, put them after the image name:
docker run --rm --entrypoint sh myimage:latest -c 'ls -l /app && id'This form runs one command and exits. It also works in scripts and CI jobs where no terminal exists.
Match the Original Environment
A reproduction is only meaningful if it matches the failing container. Copy over everything that the original docker run or Compose file supplied:
- Environment variables with
-eor--env-file - Volumes and bind mounts with
-v - Networks with
--network - The user with
--user - Memory and CPU limits, if the failure might be resource related
You can read most of these from the original container with docker inspect. If the failure disappears when you run the shell, a difference in these settings is a likely cause.
Step 3: Run the Real Command by Hand
Inside the shell, run the command you recorded in Step 1. Run the entrypoint script directly, then the command.
/entrypoint.sh node server.js
echo "exit status: $?"To see which commands a shell script runs before it fails, trace it:
sh -x /entrypoint.sh node server.jsThe -x option prints each command, with variables expanded, before it runs. The last line printed before the failure is usually the culprit.
Common Findings
Most startup failures fall into a small number of categories. Check these first.
Exit Code 127: Command Not Found
Inside the shell, check whether the executable exists and is on the PATH.
command -v node
ls -l /usr/local/bin/nodeA script can also give 127 when its interpreter is missing. A script that begins with #!/bin/bash fails with "no such file or directory" on Alpine, which has no bash by default. The file exists, but the interpreter named on the first line does not.
Windows Line Endings
A script saved with CRLF line endings has an invisible carriage return at the end of the first line. The kernel looks for an interpreter named /bin/sh\r and fails with the same "no such file or directory" error. Check for it:
head -1 /entrypoint.sh | od -c | head -2
sed -i 's/\r$//' /entrypoint.shThe sed command fixes the file inside this throwaway container only. To fix the cause, convert the file in your source tree and rebuild, or add a .gitattributes rule such as *.sh text eol=lf.
Exit Code 126: Permission Problem
The file exists but cannot be executed. Check the mode bits and whether the filesystem was mounted with noexec.
ls -l /entrypoint.sh
chmod +x /entrypoint.shAgain, chmod here only proves the diagnosis. Put the fix in the Dockerfile, for example with COPY --chmod=755 or a RUN chmod +x step.
Exit Code 1: Application Error
If the command runs and fails, the application is telling you something. Look at the error text. Typical causes are a missing environment variable, an unreachable database, or a config file that is absent. Check the environment and the files the program reads:
env | sort
ls -l /app/configWorks as Root, Fails as a Normal User
If the image sets USER, your shell already runs as that user. A failure that appears only in the original container may involve file ownership. Compare id with the owner shown by ls -l for the files the program writes to.
A Worked Example
A container named web exits immediately with code 127 and prints nothing. You follow the steps.
- You inspect the configuration and find
entrypoint=["/entrypoint.sh"]andcmd=["node","server.js"]. - You start
docker run --rm -it --entrypoint sh myimage:latest. - You run
/entrypoint.sh node server.jsand getsh: /entrypoint.sh: not found. - You run
ls -l /entrypoint.sh. The file is there, so the "not found" message refers to something else. - You run
head -1 /entrypoint.shand see#!/bin/bash. You then runcommand -v bashand get no output.
The image is Alpine-based and has no bash. The fix is either to change the first line to #!/bin/sh or to install bash in the Dockerfile. You make the change, rebuild, and the container stays up.
Edge Cases
- Images with no shell. A distroless or
scratchimage has nosh, so--entrypoint shfails with an executable-not-found error from Docker. Use the namespace-sharing technique from a later chapter, or build a debug variant of the image with a shell. - Commands that take over PID 1. Some entrypoint scripts end with
exec "$@". Theexecreplaces the script with your command, so signals and exit codes come from the application directly. When you run the script by hand in a shell, the shell is the parent and the behavior is slightly different. This rarely changes the cause, but remember it when the exit code looks odd. - Failure depends on timing. If the application fails only because a dependency such as a database is not yet ready, your manual run may succeed. Run it again against the same network and see whether the result changes.
- Overridden entrypoint hides the image's own setup. Setup done inside the entrypoint script, such as generating a config file, does not happen in your shell. You must run the script to get that setup.
Summary of the Method
- Record the entrypoint, command, user, and working directory.
- Start a throwaway container with
--entrypoint shand the same environment, mounts, and network. - Run the original command by hand, using
sh -xfor scripts. - Classify the result as missing file, permission, or application error.
- Apply the fix in the Dockerfile or configuration and rebuild.
Exercise
An image defines ENTRYPOINT ["/entrypoint.sh"] and CMD ["node", "server.js"]. You run docker run --rm -it myimage:latest sh to get a shell, but the container exits immediately with an error from the script. Why does this happen, and which command gives you a shell?
Show answer
Arguments after the image name replace CMD, not ENTRYPOINT. Docker ran /entrypoint.sh sh, so the script still ran first and failed. Use docker run --rm -it --entrypoint sh myimage:latest to replace the entrypoint itself.
Exercise
A container exits with code 127. Inside a shell started with --entrypoint sh, ls -l /entrypoint.sh shows the file, yet running it prints "not found". Name two causes and the command that checks each.
Show answer
The first line may name an interpreter that is not in the image, such as #!/bin/bash on Alpine. Check with head -1 /entrypoint.sh and command -v bash. The file may also have CRLF line endings, so the interpreter name ends in a carriage return. Check with head -1 /entrypoint.sh | od -c, and look for \r.