Docker容器在与Fleet一起启动时退出

时间:2015-02-25 23:04:06

标签: node.js docker containers coreos

我正在使用Fleet通过以下系统文件在CoreOS上启动Docker容器:

[Unit]
Description=Developer News API
After=docker.service
Requires=docker.service

[Service]
ExecStartPre=-/usr/bin/docker kill devnews-core
ExecStartPre=-/usr/bin/docker rm devnews-core
ExecStartPre=/usr/bin/docker pull imjacobclark/devnews-core
ExecStart=/usr/bin/docker run -d -p 1337:1337 --name devnews-core imjacobclark/devnews-core
ExecStop=/usr/bin/docker stop devnews-core
TimeoutStartSec=0
Restart=always
RestartSec=10s

我在这里执行的主要Docker命令是docker run -d -p 1337:1337 --name devnews-core imjacobclark/devnews-core,它在执行时独立工作,并且在1337端口上显示了所需的Web服务结果。

然而,当我执行fleetctl devnews-core.service时,Fleet声明启动已完成,但不断重复启动,这是因为在容器内的npm install,Docker退出。

通过Fleet启动服务:

core@coreos01 ~/devnews-coreos/fleet $ fleetctl start devnews-core.service 
Unit devnews-core.service launched on 8fdc9312.../

日志:

core@coreos01 ~/devnews-coreos/fleet $ fleetctl --tunnel journal devnews-core.service 
-- Logs begin at Wed 2015-02-25 13:41:25 UTC, end at Wed 2015-02-25 23:08:55 UTC. --
Feb 25 23:08:47 coreos03 docker[6404]: 44d8123e3829: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: 1e278393641a: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: ec0fa4f2b126: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: 0d98c76d34ce: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: eb0a99f0e308: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: eb0a99f0e308: Download complete
Feb 25 23:08:47 coreos03 docker[6404]: Status: Image is up to date for imjacobclark/devnews-core:latest
Feb 25 23:08:47 coreos03 systemd[1]: Started Developer News API.
Feb 25 23:08:47 coreos03 docker[6414]: 1d90cf825518610cbd15a44873c1e2640d40522632ed5417ac91f22b82c20ac4
Feb 25 23:08:47 coreos03 docker[6456]: devnews-core

说明Docker的节点失败了:

core@coreos03 ~ $ docker ps -l
CONTAINER ID        IMAGE                              COMMAND             CREATED             STATUS                       PORTS               NAMES
a5970b9f6819        imjacobclark/devnews-core:latest   "npm start"         6 seconds ago       Exited (143) 5 seconds ago                       devnews-core

此容器只是使用公共DockerHub注册表中的标准Docker Node.js容器构建的,并且在我将应用程序构建到容器中后提交。

我是否遗漏了服务文件中的任何重要内容,导致Fleet无法正确启动容器?

1 个答案:

答案 0 :(得分:2)

答案很简单,不要在妖魔化模式下启动Docker,正确的系统文件应该如下:

[Unit]
Description=Developer News API
After=docker.service
Requires=docker.service

[Service]
ExecStartPre=-/usr/bin/docker kill devnews-core
ExecStartPre=-/usr/bin/docker rm devnews-core
ExecStartPre=/usr/bin/docker pull imjacobclark/devnews-core
ExecStart=/usr/bin/docker run -p 1337:1337 --name devnews-core imjacobclark/devnews-core
ExecStop=/usr/bin/docker stop devnews-core
TimeoutStartSec=0
Restart=always
RestartSec=10s

基本上,Docker run命令应该如下:

docker run -p 1337:1337 --name devnews-core imjacobclark/devnews-core