Why does the container created with - 'docker run -d alpine sleep infinity' goes into exited/stopped state?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker is a powerful platform for developing, shipping, and running applications in containers. Containers are lightweight and can be launched in seconds, providing an isolated environment for applications to run. A common keyword you'll encounter when dealing with Docker is docker run
, which is used to create and run a container from a specified Docker image. However, at times you may observe that containers unexpectedly enter an exited or stopped state shortly after being started. This article explores why this occurs using the command docker run -d alpine sleep infinity
as an example and discusses ways to prevent it.
Docker Run Command Basics
When you invoke the command docker run
, you are essentially instructing Docker to:
- Pull the specified image from the Docker registry if not already present on your system.
- Create a new container based on that image.
- Execute the specified command within that container.
- Manage the container lifecycle based on whether or not the executed command continues to run.
The Command Breakdown:
docker run: Initiates the creation and running of a container.-d: Stands for detached mode. This runs the container in the background, allowing the terminal to be responsive immediately after the command is executed.alpine: Specifies thealpineimage, a minimal Docker image based on Alpine Linux, known for its size efficiency.sleep infinity: This command is intended to pause the execution indefinitely, keeping the container running.
Why Containers Exit
Command Lifecycle
The lifecycle of a Docker container is intrinsically tied to the command it runs. When you start a container using a specific command, the container lives only as long as that command is running. If the command finishes execution, the container will naturally exit.
Why docker run -d alpine sleep infinity
Exits
Although the intention of sleep infinity
is to keep the process running indefinitely, the container exits if there is an issue running this command, including:
- Invalid Command: Ensure that the command is correct. On some systems or environments, syntax quirks might prevent
sleep infinityfrom working as expected. - Entrypoint Misconfigurations: Alpine might not have
sleep infinityconfigured as one would expect. Validate whethersleepcan handle an "infinite" argument.
Example of Misconfigured Entrypoint
If the container doesn't have sleep
, or if the command syntax is incorrect, the container will exit immediately after creation since the initiated process never starts. This is similar to executing an invalid script—the execution halts with an error, and nothing further occurs.
Debugging Exited Containers
Debugging involves checking the logs and configuration:
- Ensure the commands and any binaries used exist within the container. For Alpine, use
sleep 9999as an alternative to simulatesleep infinity. - Trap exit signals within the container to handle shutdown gracefully in long-live processes.
- Implement logging and monitor the container output using
docker logs.

