In this tutorial, we’ll explore different ways to keep Docker containers running indefinitely.
By default, containers run only as long as their default command executes, but it’s common to run them indefinitely for debugging and troubleshooting purposes.
2. Basics of Docker Run
Let’s look at some of the basics of the docker run command, and ways to pass commands to a container when it’s started.
2.1. Specifying the Command to Run
While creating a Dockerfile, there are two ways to specify which command to run:
- The ENTRYPOINT directive specifies a command for the container. This is helpful for commands we always want to run unless the user explicitly overrides them.
- We can also specify commands using the CMD directive. This is used to define the default command or arguments to be passed to the container. Any arguments that the user includes after the image name replace the entire CMD directive.
2.2. Overriding the Default Commands
To override a CMD directive, we can simply add another command after docker run [image_name]:
docker run ubuntu echo "Hello World"
To override the ENTRYPOINT directive, we need to add the –entrypoint flag and the desired command before the image name, and any arguments after the image name:
docker run --entrypoint echo ubuntu "Hello World"
Both the examples will run the command echo “Hello World” when the container starts.
2.3. Docker Run With a Command
The default behavior of the docker run command can be summarized by the following:
- The container runs in the foreground unless explicitly detached using the -d flag.
- The container runs as long as the specified command keeps running and then stops.
Let’s look at an example of this:
docker run ubuntu bash
The above command will run the bash command in the ubuntu image. The bash command will start a shell in the container.
The command runs, but the container stops after the command finishes, which is almost immediately. We can test this using the docker ps command:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8f8f8f8f8f8 ubuntu bash 2 minutes ago Exited (0) 22/tcp mystifying_snyder
3. Running Docker Containers Indefinitely
By default, a container may or may not be designed to run indefinitely. For example, a container containing a web server runs as long as the web server is running. However, a container containing a one-time job, such as a cron job, runs only for a short period of time.
Let’s look at some ways to run containers indefinitely.
3.1. Never Ending Commands
The simplest way to keep the container running is to pass a command that never ends.
We can use the tail -f command to read the /dev/null file. The command keeps looking for new changes in the file to display, so it never ends as long as the file exists:
docker run ubuntu tail -f /dev/null
We can use the below command to run an infinite loop that does nothing:
docker run ubuntu while true; do sleep 1; done
The below command keeps the container idle and does nothing:
docker run ubuntu sleep infinity
We can use never-ending commands in any of the following ways:
- ENTRYPOINT or CMD directive in the Dockerfile
- Overriding ENTRYPOINT or CMD in the docker run command
It doesn’t make sense to run a never-ending command in the foreground and get a stuck terminal. In this case, we can use the -d flag to run the container in the background.
3.2. Starting a Pseudo-TTY
Never-ending commands were a hack a long time ago when no other options existed. However, in the latest versions of Docker, it’s possible to keep containers running by starting a terminal session with them both in the foreground and the background.
Pseudo-TTYs are used to run commands inside a container. To start a pseudo-TTY session with the container, we can use the -t flag. The container won’t exit until the session ends.
If we want to interact with the container, we can couple this with the -i flag. This will allow us to run commands in the container using our terminal:
docker run -it ubuntu bash
Alternatively, if our purpose is just to run the container indefinitely, we can use the -d flag:
docker run -d -t ubuntu
In this article, we learned some ways to run Docker containers indefinitely. We discussed the ways commands are passed to a container, and then altered these commands to solve our problem. We also touched on the modern solution of using Pseudo-TTY sessions to keep containers running.