简体   繁体   English

为什么我的一个Docker容器在机器启动时无法自动启动?

[英]Why does one of my Docker containers not start automatically on machine boot?

I have two Docker containers, one to run a Jenkins instance, and one to run YouTrack . 我有两个Docker容器,一个用于运行Jenkins实例,另一个用于运行YouTrack Their respective starting scripts look like the following: 它们各自的启动脚本如下所示:

  • Jenkins: docker run --name jenkins_master --restart on-failure -p 8080:8080 -p 50000:50000 -v /home/ci/jenkins_home/:/var/jenkins_home -d jenkins:latest Jenkins: docker run --name jenkins_master --restart on-failure -p 8080:8080 -p 50000:50000 -v /home/ci/jenkins_home/:/var/jenkins_home -d jenkins:latest

  • YouTrack: docker run --name youtrack --restart on-failure -p 8081:80 -v /home/ci/youtrack/data/:/opt/youtrack/data/ -v /home/ci/youtrack/backup/:/opt/youtrack/backup -d uniplug/youtrack YouTrack:docker docker run --name youtrack --restart on-failure -p 8081:80 -v /home/ci/youtrack/data/:/opt/youtrack/data/ -v /home/ci/youtrack/backup/:/opt/youtrack/backup -d uniplug/youtrack

As you can see, nothing special, some port mapping and some -v . 如您所见,没有什么特别之处,有一些端口映射和一些-v

I would like them to start running when I boot up the PC. 我希望它们在启动PC时开始运行。 The Docker documenation says " Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. " Docker文档指出:Docker提供了重启策略来控制您的容器在退出时还是在Docker重新启动时自动启动。

As Docker "restarts" when I boot up my machine, I assumed both containers to run on boot due to --restart on-failure . 当我启动机器时Docker“重新启动”时,由于--restart on-failure ,我假定两个容器都在启动时运行。 But only Jenkins starts to run on port 8080, I have to manually start YouTrack when I reboot my machine. 但是只有Jenkins开始在端口8080上运行,重新启动计算机时,我必须手动启动YouTrack。

How can I avoid manually starting the container? 如何避免手动启动容器?

When you reeboot your PC, what the docker daemon does is try to stop the running containers. 重新启动PC时,docker守护程序执行的操作是尝试停止正在运行的容器。 This is like running docker stop on all running container, and this is what happens (taken from here ): 这就像在所有正在运行的容器上运行docker stop ,这就是发生的情况(从此处获取 ):

When you issue a docker stop command Docker will first ask nicely for the process to stop and if it doesn't comply within 10 seconds it will forcibly kill it. 当您发出docker stop命令时,Docker首先会很好地要求停止该过程,如果它在10秒内不符合要求,它将强行杀死它。 If you've ever issued a docker stop and had to wait 10 seconds for the command to return you've seen this in action 如果您曾经发布过docker stop并且必须等待10秒才能返回命令,那么您已经看到了这一点

The docker stop command attempts to stop a running container first by sending a SIGTERM signal to the root process (PID 1) in the container. docker stop命令首先尝试通过向容器中的根进程(PID 1)发送SIGTERM信号来停止正在运行的容器。 If the process hasn't exited within the timeout period a SIGKILL signal will be sent. 如果该进程在超时时间内仍未退出,则将发送SIGKILL信号。

For your containers, you specified --restart on-failure that means that the docker daemon will restart your containers only if the exit status is > 0 when they exit. 对于您的容器,您指定了--restart on-failure ,这意味着--restart on-failure守护程序仅在退出时退出状态> 0时才重新启动容器。 My guess about your issue is that your youtrack container correctly reacts to the SIGTERM signal given by the docker daemon and exits cleanly (exit status 0). 我对您的问题的猜测是,您的youtrack容器正确响应docker守护程序给出的SIGTERM信号,并干净退出(退出状态0)。 On the opposite side, the jenkins container does not exit cleanly. 在相反的一侧,詹金斯容器不能干净地退出。 For this reason, only the jenkins container restarts on reboot. 因此,只有jenkins容器会在重新引导时重新启动。

To solve this problem you can run the containers with the --restart always flag and the containers will restart in any case. 要解决此问题,您可以使用--restart always标志运行容器, --restart always容器都将重新启动。

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

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