How to Run Containers Continuously in the Background Using Docker

How to Run Containers Continuously in the Background Using Docker

Docker automatically exits a container when executed in the background; however, there will be certain instances where we need to ensure that the instance continues to run despite the process being finished. This article will discuss how we can continue running docker containers in the background.

Run Docker Containers in the Background

If we want to simplify the way to run containers, we should run our container in the background with the following:

docker run -d image sample

Instead of running with the one below:

docker run -i -t image sample

Using -d is recommended because we can run our container with just one command, and we don’t need to detach the container’s terminal by hitting Ctrl+P + Q.

However, there is a problem with the -d parameter. As a result, our container immediately stops unless the commands are not running in the foreground.

Let us explain this using a case where we want to run an Apache Service on a container. The intuitive way of doing this is:

docker run -d apache-server apachectl start

However, the started container stops immediately because apachectl exits once it detaches the Apache Daemon.

Docker doesn’t like this. Docker requires our command to keep running in the foreground; otherwise, it thinks that our application stops and shutdown the container.

Run Containers in the Foreground

We can solve this by directly running the apache executable with the foreground option.

docker run -d apache-server /usr/sbin/apache2 -D NO_DETACH -D FOREGROUND

Here, we manually do what apachectl does for us and run the apache executable. With this approach, apache keeps running in the foreground.

Again, remember that the process differs for every Unix-based image.

The problem is that some application doesn’t run in the foreground. Also, we need to do extra work, such as exporting environment variables by ourselves.

In this situation, we can add tail -f /dev/null to your command. Our container doesn’t stop because the tail keeps running in the foreground.

We can use this technique in our previous case.

docker run -d apache-server apachectl start && tail -f /dev/null

Since tail -f /dev/null doesn’t harm us, we can use this workaround for any applications.

Run Containers Indefinitely

Another simple way to keep a container alive indefinitely in daemon -d mode is to run the sleep infinity as the container’s argument. This is independent of performing odd actions like allocating a TTY in daemon mode.

Although the command is peculiar, such as making sleep your default command.

docker run -d apache-server sleep infinity

Also, the -t parameter allocates a pseudo-tty and should do the trick too. This manipulates bash into continuing to run because it thinks it is connected to an interactive TTY.

docker run -t -d apache-server
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - Docker Container