简体   繁体   English

在整个 docker 构建过程中保持一个进程运行

[英]Keep a process running throughout docker build process

I'm trying to keep a devpi instance running inside a container throughout the build process so subsequent RUN commands can make use of it and populate it's database during the build.我试图在整个构建过程中保持一个 devpi 实例在容器内运行,以便后续的 RUN 命令可以在构建过程中使用它并填充它的数据库。 eg例如

FROM centos:centos7 
RUN pip install devpi
RUN devpi-server --host=0.0.0.0 --port=3141
RUN some other task that interacts with devpi-server
...

Is this possible?这可能吗? I've been unable to get it working so far到目前为止我一直无法让它工作

I think if you want to run a certain command in the background you might need to run:我认为如果您想在后台运行某个命令,您可能需要运行:

nohup command &

but in your case since you want to run it in background during image build time this might help you:但在您的情况下,因为您想在图像构建期间在后台运行它,这可能会对您有所帮助:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y inetutils-ping
# Using sleep will give the command some time to be executed completely
RUN sleep 10 ; ping localhost -c 3 > xxx.log

As you can see I am running a sleep command and ping command simultaneously by separating them with ;如您所见,我正在同时运行 sleep 命令和 ping 命令,方法是用;分隔它们。 let's say in your example the estimated time to populate its database during the build might take around 2mins so setting sleep time close to that interval would solve ur problem.假设在您的示例中,在构建期间填充其数据库的估计时间可能需要大约2 分钟,因此将睡眠时间设置为接近该时间间隔将解决您的问题。

PS: Make sure to know the difference between RUN and CMD, since docker build will actually create a temporary container to test the RUN commands then remove it once the build is done. PS:确保知道 RUN 和 CMD 之间的区别,因为 docker 构建实际上会创建一个临时容器来测试 RUN 命令,然后在构建完成后将其删除。

I figured it out.我想到了。 I needed to add a seperate shell script that both started devpi-server and performed the commands that interacted with it.我需要添加一个单独的 shell 脚本,该脚本既启动了 devpi-server 又执行了与之交互的命令。 Then I can start the process and interact with it within the same RUN command.然后我可以启动进程并在同一个 RUN 命令中与之交互。

FROM centos:centos7 
RUN pip install devpi
ADD start-devpi.sh
RUN chmod +x start-devpi.sh
RUN ./start-devpi.sh

Where start-devpi.sh looks like start-devpi.sh 的样子

devpi-server --host=0.0.0.0 &
sleep 15 #wait for the server to come online
put further commands that use the running devpi-server instance here
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM