Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: April 20, 2024
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.
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.
While creating a Dockerfile, there are two ways to specify which command to run:
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.
The default behavior of the docker run command can be summarized by the following:
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
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.
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:
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.
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.