使用 Docker 在后台连续运行容器
Docker在后台执行时会自动退出一个容器; 但是,在某些情况下,我们需要确保尽管进程已完成,实例仍会继续运行。 本文将讨论如何在后台继续运行 docker 容器。
在后台运行 Docker 容器
如果我们想简化运行容器的方式,我们应该在后台运行我们的容器,如下所示:
$ docker run -d image sample
而不是使用下面的运行:
$ docker run -i -t image sample
推荐使用 -d,因为我们可以只用一个命令运行我们的容器,我们不需要通过按 Ctrl+P + Q 来分离容器的终端。
但是,**-d** 参数有问题。 结果,除非命令不在前台运行,否则我们的容器会立即停止。
让我们用一个我们想在容器上运行 Apache 服务的案例来解释这一点。 这样做的直观方法是:
$ docker run -d apache-server apachectl start
但是,启动的容器会立即停止,因为一旦分离 Apache 守护进程,apachectl 就会退出。
Docker 不喜欢这样。 Docker 要求我们的命令一直在前台运行; 否则,它认为我们的应用程序停止并关闭了容器。
在前台运行容器
我们可以通过使用前台选项直接运行 apache 可执行文件来解决这个问题。
$ docker run -d apache-server /usr/sbin/apache2 -D NO_DETACH -D FOREGROUND
在这里,我们手动执行 apachectl 为我们执行的操作并运行 apache 可执行文件。 通过这种方法,apache 一直在前台运行。
同样,请记住每个基于 Unix 的映像的过程都不同。
问题是某些应用程序不在前台运行。 另外,我们还需要做一些额外的工作,比如我们自己导出环境变量。
在这种情况下,我们可以将 tail -f /dev/null
添加到您的命令中。 我们的容器不会停止,因为 tail 一直在前台运行。
我们可以在之前的案例中使用这种技术。
$ docker run -d apache-server apachectl start && tail -f /dev/null
由于 tail -f /dev/null
不会对我们造成伤害,我们可以将此解决方法用于任何应用程序。
无限期地运行容器
在 daemon -d
模式下使容器无限期保持活动状态的另一种简单方法是运行 sleep infinity 作为容器的参数。 这与执行奇怪的操作无关,例如在守护进程模式下分配 TTY。
虽然这个命令很奇特,比如让 sleep 成为你的默认命令。
$ docker run -d apache-server sleep infinity
此外,-t 参数分配一个伪 tty,也应该可以解决问题。 这会操纵 bash 继续运行,因为它认为它已连接到交互式 TTY。
$ docker run -t -d apache-server
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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