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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial