我有两个 Docker 容器,一个用于运行 Jenkins 实例,另一个用于运行 YouTrack。它们各自的起始脚本如下所示:
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 :最新的
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
如您所见,没什么特别的,一些端口映射和一些-v
。
我希望它们在我启动 PC 时开始运行。 The Docker documenation says “Docker 提供重启策略来控制您的容器是在退出时自动启动,还是在 Docker 重启时自动启动。”
当我启动我的机器时 Docker 会“重新启动”,由于 --restart on-failure
,我假设两个容器都会在启动时运行。但是只有 Jenkins 开始在端口 8080 上运行,我必须在重启机器时手动启动 YouTrack。
如何避免手动启动容器?
最佳答案
当您重新启动您的 PC 时,docker 守护进程所做的是尝试停止正在运行的容器。这就像在所有正在运行的容器上运行 docker stop
,这就是发生的事情(取自 here ):
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. 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
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. If the process hasn't exited within the timeout period a SIGKILL signal will be sent.
对于您的容器,您指定了 --restart on-failure
这意味着只有当容器退出时退出状态 > 0 时,docker 守护进程才会重启您的容器。我对你的问题的猜测是你的 youtrack 容器正确地响应了 docker 守护进程给出的 SIGTERM 信号并干净地退出(退出状态 0)。另一方面,jenkins 容器并没有干净地退出。因此,只有 jenkins 容器在重启时重启。
要解决此问题,您可以使用 --restart always
标志运行容器,容器将在任何情况下重新启动。
关于linux - 为什么我的 Docker 容器之一没有在机器启动时自动启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46090592/