在遵循系统指南我在此示例配置中遇到了问题:
[Unit]
Description=MyApp
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill busybox1
ExecStartPre=-/usr/bin/docker rm busybox1
ExecStartPre=/usr/bin/docker pull busybox
ExecStart=/usr/bin/docker run --name busybox1 busybox /bin/sh -c "while true; do echo Hello World; sleep 1; done"
[Install]
WantedBy=multi-user.target
如果我在系统运行时运行“systemctl start hello”,docker 将启动,但如果我启用“hello.service”在启动时通过 systemd 启动并重新启动机器,我会收到此错误:
-- Reboot --
Mar 15 01:17:44 general systemd[1]: Starting MyApp...
Mar 15 01:17:47 general docker[510]: Error response from daemon: No such container: busybox1
Mar 15 01:17:47 general docker[510]: time="2015-03-15T01:17:47Z" level="fatal" msg="Error: failed to kill one or more containers"
Mar 15 01:17:47 general docker[637]: Error response from daemon: No such container: busybox1
Mar 15 01:17:47 general docker[637]: time="2015-03-15T01:17:47Z" level="fatal" msg="Error: failed to remove one or more containers"
Mar 15 01:17:47 general systemd[1]: hello.service: control process exited, code=exited status=1
Mar 15 01:17:47 general systemd[1]: Failed to start MyApp.
Mar 15 01:17:47 general systemd[1]: Unit hello.service entered failed state.
Mar 15 01:17:47 general systemd[1]: hello.service failed.
Mar 15 01:17:47 general docker[673]: Pulling repository busybox
Mar 15 01:17:47 general docker[673]: time="2015-03-15T01:17:47Z" level="fatal" msg="Get https://index.docker.io/v1/repositories/library/busybox/images: dial tcp: lookup index.docker.io: connection refused"
知道我做错了什么吗?
答案1
如果您想在启动时运行它,您应该依赖该network-online.target
服务。在运行该命令之前,您需要确保网络已启动docker pull
。看 ”网络通后运行服务“来自 systemd 文档以获取更多信息。
以下对我有用:
[Unit]
Description=MyApp
Requires=docker.service network-online.target
After=docker.service network-online.target
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill busybox1
ExecStartPre=-/usr/bin/docker rm busybox1
ExecStartPre=/usr/bin/docker pull busybox
ExecStart=/usr/bin/docker run --name busybox1 busybox /bin/sh -c "while true; do echo Hello World; sleep 1; done"
[Install]
WantedBy=multi-user.target
答案2
我尝试了建议的:
Requires=docker.service network-online.target
After=docker.service network-online.target
但这没有用。我有:
-- Reboot --
Feb 09 23:20:24 coreos-512mb-fra1-01 systemd[1]: Starting New Relic Linux Server Monitor (nrsysmond)...
Feb 09 23:20:24 coreos-512mb-fra1-01 docker[782]: Failed to kill container (nrsysmond): Error response from daemon: Cannot kill container nrsysmond: Container c608c10f44c06c550492e872773b0d5a59a7b86e639f63487f6186983a4f786b is not running
Feb 09 23:20:25 coreos-512mb-fra1-01 docker[787]: nrsysmond
Feb 09 23:20:25 coreos-512mb-fra1-01 docker[794]: Pulling repository docker.io/newrelic/nrsysmond
Feb 09 23:20:25 coreos-512mb-fra1-01 docker[794]: Error while pulling image: Get https://index.docker.io/v1/repositories/newrelic/nrsysmond/images: dial tcp: lookup index.docker.io: Temporary failure in name resolution
Feb 09 23:20:25 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Control process exited, code=exited status=1
Feb 09 23:20:25 coreos-512mb-fra1-01 systemd[1]: Failed to start New Relic Linux Server Monitor (nrsysmond).
Feb 09 23:20:25 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Unit entered failed state.
Feb 09 23:20:25 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Failed with result 'exit-code'.
添加Restart
并RestartSec
修复问题:
# Restart after crash
Restart=always
# Give the service 10 seconds to recover after the previous restart
RestartSec=10s
它不优雅但有效。这是日志:
Feb 09 23:23:57 coreos-512mb-fra1-01 systemd[1]: Starting New Relic Linux Server Monitor (nrsysmond)...
Feb 09 23:23:57 coreos-512mb-fra1-01 docker[792]: Failed to kill container (nrsysmond): Error response from daemon: Cannot kill container nrsysmond: Container 31fb78809
Feb 09 23:23:57 coreos-512mb-fra1-01 docker[797]: nrsysmond
Feb 09 23:23:57 coreos-512mb-fra1-01 docker[804]: Pulling repository docker.io/newrelic/nrsysmond
Feb 09 23:23:57 coreos-512mb-fra1-01 docker[804]: Error while pulling image: Get https://index.docker.io/v1/repositories/newrelic/nrsysmond/images: dial tcp: lookup ind
Feb 09 23:23:57 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Control process exited, code=exited status=1
Feb 09 23:23:57 coreos-512mb-fra1-01 systemd[1]: Failed to start New Relic Linux Server Monitor (nrsysmond).
Feb 09 23:23:57 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Unit entered failed state.
Feb 09 23:23:57 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Failed with result 'exit-code'.
Feb 09 23:24:08 coreos-512mb-fra1-01 systemd[1]: newrelic.service: Service hold-off time over, scheduling restart.
Feb 09 23:24:08 coreos-512mb-fra1-01 systemd[1]: Stopped New Relic Linux Server Monitor (nrsysmond).
Feb 09 23:24:08 coreos-512mb-fra1-01 systemd[1]: Starting New Relic Linux Server Monitor (nrsysmond)...
Feb 09 23:24:08 coreos-512mb-fra1-01 docker[869]: Failed to kill container (nrsysmond): Error response from daemon: Cannot kill container nrsysmond: No such container:
Feb 09 23:24:08 coreos-512mb-fra1-01 docker[875]: Failed to remove container (nrsysmond): Error response from daemon: No such container: nrsysmond
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: latest: Pulling from newrelic/nrsysmond
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: 6ffe5d2d6a97: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: f4e00f994fd4: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: e99f3d1fc87b: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: a3ed95caeb02: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: a3ed95caeb02: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: 65cdb07f703d: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: a3ed95caeb02: Already exists
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: Digest: sha256:c184f97452321baa0b0ee4ee402e3aaa204f041beb7a71a347db6c4efecba07f
Feb 09 23:24:10 coreos-512mb-fra1-01 docker[883]: Status: Image is up to date for newrelic/nrsysmond:latest
Feb 09 23:24:10 coreos-512mb-fra1-01 systemd[1]: Started New Relic Linux Server Monitor (nrsysmond).
自从我在 DigitalOcean CoreOS 上遇到这个问题后,我在他们的资源中寻找帮助。有一个线与此问题相关。它建议这样:
After=early-docker.service systemd-networkd-wait-online.service
Requires=early-docker.service systemd-networkd-wait-online.service
Before=early-docker.target
但这些行导致 Docker 挂起/冻结。我不得不终止与我的服务相关的所有进程,然后重新启动 Docker 服务以使其再次响应。
码头工人版本:
docker -v
Docker version 1.10.0, build e21da33