JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Using Docker to continuously run containers in the background

Author:JIYIK Last Updated:2025/03/25 Views:

Docker will automatically exit a container when it is executed in the background; however, in some cases, we need to ensure that the instance continues to run despite the process being completed. This article will discuss how to continue running a docker container in the background.


Running Docker containers in the background

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

$ docker run -d image sample

Instead of running:

$ docker run -i -t image sample

It is recommended to use -d because we can run our container with only one command and we don’t need to detach the container’s terminal by pressing Ctrl+P + Q.

However, there is something wrong with the -d parameter. As a result, our container stops immediately unless the command is not running in the foreground.

Let's explain this with a case where we want to run an Apache service on a container. The intuitive way to do this is:

$ docker run -d apache-server apachectl start

However, the started container stops immediately because apachectl exits once the Apache daemon is detached.

Docker doesn’t like this. Docker requires our command to be kept running in the foreground; otherwise, it thinks our application is stopped and shuts down the container.


Running a container in the foreground

We can solve this problem by running the apache executable directly 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 runs the apache executable. In this way, apache is always running in the foreground.

Again, keep in mind that the process is different for each Unix-based image.

The problem is that some applications do not run in the foreground. Also, we need to do some extra work, such as exporting the environment variables ourselves.

In this case, we can tail -f /dev/nulladd to your command. Our container will not stop because tail is always running in the foreground.

We can use this technique in the previous example.

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

Since tail -f /dev/nullit does not harm us, we can use this workaround for any application.


Run the container indefinitely

daemon -dAnother simple way to keep a container alive indefinitely in daemon mode is to run sleep infinity as an argument to the container. This has nothing to do with doing weird things like allocating a TTY in daemon mode .

Although this command is very fancy, like making sleep your default command.

$ docker run -d apache-server sleep infinity

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

$ docker run -t -d apache-server

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial