Quick revision
The short answer
docker ps only shows running containers, so we start with docker ps -a to find the exited container and its exit code, then docker logs to read its startup output, which is kept after exit. docker inspect shows State.ExitCode, OOMKilled, the command and the environment. Common causes are missing configuration, a database URL pointing at localhost (which inside a container is the container itself), a wrong ENTRYPOINT, or memory limits; exit code 137 points to a kill, often out of memory, and 1 to an application error. Running the image with docker run -it --entrypoint sh lets us try the start command by hand.
What to remember
In this answer, we saw that a container stops when its main process exits, so docker ps -a and docker logs are the first tools, and docker inspect shows the exit code, the OOMKilled flag, the command and the environment. We read exit codes (1 for application errors, 137 for kills, 126 and 127 for bad commands), went through the common causes (missing configuration, localhost meaning the container, a wrong entrypoint, memory limits) and reproduced the failure interactively with --entrypoint sh.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
docker ps only shows running containers, so we start with docker ps -a to find the exited container and its exit code, then docker logs to read its startup output, which is kept after exit. docker inspect shows State.ExitCode, OOMKilled, the command and the environment. Common causes are missing configuration, a database URL pointing at localhost (which inside a container is the container itself), a wrong ENTRYPOINT, or memory limits; exit code 137 points to a kill, often out of memory, and 1 to an application error. Running the image with docker run -it --entrypoint sh lets us try the start command by hand.
Why this question is asked
Works on my laptop, dies in a container is one of the most common problems with containerised Spring Boot apps, and it is confusing because docker ps shows nothing at all. Interviewers ask it to see whether we debug methodically with the tools Docker gives us, and whether we know the handful of differences between a laptop and a container that cause most of these failures. In this answer, we will see why the container disappears from docker ps, read the exit code and logs, inspect the container's state, go through the common causes with the fix for each, and finish with an interactive way to reproduce the failure.
1Why docker ps Shows Nothing
A container lives exactly as long as its main process, the command from ENTRYPOINT or CMD. When the Java process exits, for whatever reason, the container stops. docker ps lists only running containers, so it shows nothing. But the stopped container still exists, with its logs and state, until someone removes it. That is good news: all the evidence is still there. The one way to lose it is to start the container with --rm, which deletes it on exit, so we leave that flag off while debugging.
2Find the Container and Read Its Logs
docker ps -a lists stopped containers too, with a status that includes the exit code. docker logs then prints everything the process wrote to stdout and stderr, including Spring Boot's startup failure report, even though it is no longer running.
In most cases this is where the investigation ends: Spring Boot prints a clear failure analysis. Notice the exit code 1 in the status; we will come back to what the codes mean.
3Inspect the Container's State
docker inspect prints the full JSON description of the container. The useful parts are State (exit code, whether the kernel's OOM killer ended it, the error and timestamps), Config.Entrypoint and Config.Cmd (what was actually run) and Config.Env (the environment it got). A Go template picks out just the fields we need.
| Exit code | Usual meaning | Look at |
|---|---|---|
| 0 | process finished normally | app not meant to keep running? (no web server) |
| 1 | application error | docker logs, Spring failure report |
| 126 / 127 | command not executable / not found | ENTRYPOINT, file paths, permissions |
| 137 | killed with SIGKILL (128 + 9) | OOMKilled flag, memory limit |
| 143 | terminated with SIGTERM (128 + 15) | someone stopped it |
Comparing the environment here with the laptop's shows quickly what the container is missing.
4The Common Causes and Their Fixes
Missing configuration. On the laptop, the IDE or a shell profile sets environment variables, an application-local.yml is on the classpath, or the dev profile is active. The container has none of that. The fix is to pass configuration explicitly with -e or --env-file, and to make sure the right profile is active.
localhost means the container itself. A datasource URL of jdbc:postgresql://localhost:5432/orders works on the laptop because the database runs there. Inside a container, localhost is the container's own network interface, where nothing listens on 5432, so the connection is refused and Spring fails to start. On Docker Desktop we can use host.docker.internal to reach the host; better, we run the database as another container on a user-defined network and refer to it by its name.
Wrong ENTRYPOINT or file path. A typo in the jar path, a jar copied to a different folder, or a shell-form command with wrong quoting gives exit code 126 or 127, or a Java error such as Unable to access jarfile. docker inspect shows exactly what was run.
Memory limits. If the container runs with --memory 512m and the heap or the total JVM memory needs more, the kernel kills the process with SIGKILL. The status shows 137 and OOMKilled is true. Sizing the heap with -XX:MaxRAMPercentage, or raising the limit, fixes it. Exit code 0 is a last, subtler case: a Spring Boot app without a web starter has nothing to keep it alive and simply finishes.
5Reproducing It by Hand
When the logs are not enough, for example when the process dies before writing anything, we start the same image with a shell instead of the application. --entrypoint replaces the image's ENTRYPOINT, and -it gives us an interactive terminal. Inside, we can check that the jar is where we expect, look at the environment, and run the exact start command to watch it fail.
Here --rm is fine, because this throwaway container is only for experiments. If the image has no shell, as with distroless images, we can use its debug variant or copy files out with docker cp.
6Follow-up questions
Why does the app work with java -jar on the laptop but not in the container?
Because the laptop provides things the container does not: environment variables, local config files, an active profile, a database on localhost, more memory and a different Java version. The image only contains what the Dockerfile put in it and the flags we pass at run time, so each difference has to be made explicit.
How do I tell an out-of-memory kill from a Java OutOfMemoryError?
A kill by the kernel gives exit code 137 and OOMKilled true, usually with no Java error in the logs, because the process was stopped from outside. A Java OutOfMemoryError is thrown inside the JVM, appears in the logs as java.lang.OutOfMemoryError, and typically ends with exit code 1 unless configured otherwise.
What if docker logs is empty?
Then the process probably failed before Java started, for example a wrong entrypoint, a missing executable or a wrong architecture image. docker inspect for the command and State.Error, and an interactive run with --entrypoint sh, are the next steps.
7Conclusion
In this answer, we saw that a container stops when its main process exits, so docker ps -a and docker logs are the first tools, and docker inspect shows the exit code, the OOMKilled flag, the command and the environment. We read exit codes (1 for application errors, 137 for kills, 126 and 127 for bad commands), went through the common causes (missing configuration, localhost meaning the container, a wrong entrypoint, memory limits) and reproduced the failure interactively with --entrypoint sh.
Open with
The container is gone from docker ps because a container only lives as long as its main process, and docker ps only shows running ones. So I would run docker ps -a to find it and see its exit code, then docker logs on it, because the logs survive after exit and Spring Boot usually prints a clear failure report.
Then, if they want more
Then docker inspect, looking at State.ExitCode, State.OOMKilled, the entrypoint and command that were actually run, and the environment variables. Exit code 1 means the application failed, 137 means it was killed with SIGKILL, often by the OOM killer if OOMKilled is true, and 126 or 127 means the command could not run.
The common causes are missing configuration, since the laptop had environment variables or a local profile that the container does not; a datasource URL pointing at localhost, which inside a container is the container itself, so the database should be another container on a shared network reached by its name; a wrong ENTRYPOINT or jar path; and a memory limit smaller than what the JVM needs.
If the logs are not enough, I would start the image with docker run -it --entrypoint sh, check the files and environment, and run the java command by hand to see it fail.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot container exits immediately after docker run. docker ps shows nothing, and...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/e93ec674e8/