Why does a Docker container exit immediately after running docker run -d ubuntu?

Asked 20 hours ago 31 views

0

I encountered a scenario during a hands-on technical assessment where I launched an Ubuntu container in detached mode using the following command:

# Launch an Ubuntu container in background mode
docker run -d --name test-container ubuntu

# List all containers to inspect status
docker ps -a

The output showed that the container was created, but immediately exited with code 0 instead of running continuously in the background. The interviewer asked me to explain the root cause of this behavior and demonstrate how to keep the container running.

I want to make sure I fully grasp the core concepts behind this question for future interviews. Specifically:

  • How does Docker determine the lifecycle of a container based on its foreground Process ID 1 (PID 1)?
  • Why does a default base image like ubuntu or alpine exit when no foreground command is specified?
  • What are the standard techniques (e.g., passing interactive flags or overriding the default entrypoint/cmd) used to keep a utility container running during testing or debugging?

0 Answers


Write Your Answer