比如 mysql 的容器,自己手动运行命令docker start blog_mysql
就没问题,用systemctl start blog_mysql.service
启动的容器会马上自动异常退出
[Unit]
Description=docker blog mysql
Requires=docker.service network-online.target
After=docker.service network-online.target
[Service]
ExecStart=docker start blog_mysql
ExecStop=docker stop blog_mysql
ExecReload=docker blog_mysql
Type=simple
Restart=on-failure
[Install]
WantedBy=multi-user.target
1
kaneg 2021-02-12 16:30:43 +08:00 via iPhone
为什么要用 systemd 来启动 docker 呢?如果你想系统启动的时候自动启动 docker 容器,docker 有自己的参数,restart-policy 为 always 即可
|
3
boris93 2021-02-12 18:13:40 +08:00 via Android
或者用 docker-compose 管理也行,配置文件写个 restart: always 就行
|
4
Meltdown OP @boris93 开机启动的目的已经达到了,只是现在还奇怪为什么敲命令可以直接执行成功的,到 systemd 这里就不行了,看 dockerd 的日志也没看出什么名堂
|
5
Kobayashi 2021-02-12 20:23:03 +08:00 via Android
因为 docker 就是这么工作的。容器就是进程,进程结束容器退出。是本完整的 docker 教程应该都有讲吧。
https://yeasy.gitbook.io/docker_practice/image/dockerfile/cmd |
6
Kobayashi 2021-02-12 20:36:03 +08:00 via Android
理解错了,你是要 systemd 从外部控制容器启动。感觉情况差不多,systemd 也是要求被调用程序前台运行,或者你应该试试 systemd oneshot 。
|
8
mason961125 2021-02-13 00:36:55 +08:00
你至少也 systemctl status xx.service 或者 journalctl -u xx.service 发一下日志吧...
|
9
mason961125 2021-02-13 00:37:47 +08:00
@Meltdown -d 是 detach 啊,这个时候 docker start 就退出了,容器进程交给 dockerd 管理,肯定达不到你让 systemd 管理的要求。
|
10
ManjusakaL 2021-02-13 01:29:03 +08:00
最起码发一下具体的日志吧。。。
|
11
Meltdown OP journalctl -u xx.service
>2 月 13 09:32:56 xx systemd[1]: Started docker blog mysql. >2 月 13 09:32:57 xx docker[1872880]: blog_mysql >2 月 13 09:33:09 xx docker[1872988]: blog_mysql >2 月 13 09:33:09 xx systemd[1]: blog_mysql.service: Succeeded. docker ps -a >CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES >675f25a5a8b9 mysql "docker-entrypoint.s…" 3 weeks ago Exited (137) 2 seconds ago blog_mysql 之前创建这个容器的命令: ``` docker run --name blog_mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag ``` |
12
lululau 2021-02-13 09:46:20 +08:00 via iPhone
systemctl start mysql/mariadb 不香吗
|
14
Meltdown OP journalctl -xe
-- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:32:39 xx systemd[1]: run-docker-runtime\x2drunc-moby-3918c86a3cc9c5bec18bd10e8423336f6e2d07a2ed4e6a4a72bcdae1c9d0a134-runc.zswgif.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit run-docker-runtime\x2drunc-moby-3918c86a3cc9c5bec18bd10e8423336f6e2d07a2ed4e6a4a72bcdae1c9d0a134-runc.zswgif.mount has successfully entered the 'dead' state. 2 月 13 09:32:39 xx systemd[1659]: run-docker-runtime\x2drunc-moby-3918c86a3cc9c5bec18bd10e8423336f6e2d07a2ed4e6a4a72bcdae1c9d0a134-runc.zswgif.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:32:39 xx systemd[3813]: run-docker-runtime\x2drunc-moby-3918c86a3cc9c5bec18bd10e8423336f6e2d07a2ed4e6a4a72bcdae1c9d0a134-runc.zswgif.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:32:52 xx polkitd(authority=local)[1299]: Registered Authentication Agent for unix-process:1872840:63220323 (system bus name :1.875 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object p> 2 月 13 09:32:52 xx systemd[1]: Started Run anacron jobs. -- Subject: anacron.service 单元已结束启动 -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- anacron.service 单元已结束启动。 -- -- 启动结果为“done”。 2 月 13 09:32:52 xx anacron[1872847]: Anacron 2.3 started on 2021-02-13 2 月 13 09:32:52 xx anacron[1872847]: Normal exit (0 jobs run) 2 月 13 09:32:52 xx systemd[1]: anacron.service: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- anacron.service 单元已结束启动。 -- anacron.service 单元已结束启动。 -- -- 启动结果为“done”。 2 月 13 09:32:52 xx anacron[1872847]: Anacron 2.3 started on 2021-02-13 2 月 13 09:32:52 xx anacron[1872847]: Normal exit (0 jobs run) 2 月 13 09:32:52 xx systemd[1]: anacron.service: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit anacron.service has successfully entered the 'dead' state. 2 月 13 09:32:56 xx polkitd(authority=local)[1299]: Operator of unix-session:2 successfully authenticated as unix-user:aaa to gain TEMPORARY authorization for action org.freedesktop.systemd1.manage-> 2 月 13 09:32:56 xx systemd[1]: Started docker blog mysql. -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit anacron.service has successfully entered the 'dead' state. 2 月 13 09:32:56 xx polkitd(authority=local)[1299]: Operator of unix-session:2 successfully authenticated as unix-user:aaa to gain TEMPORARY authorization for action org.freedesktop.systemd1.manage-> 2 月 13 09:32:56 xx systemd[1]: Started docker blog mysql. -- Subject: blog_mysql.service 单元已结束启动 -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- blog_mysql.service 单元已结束启动。 -- -- 启动结果为“done”。 |
15
Meltdown OP journalctl -xe
2 月 13 09:32:56 xx kernel: docker0: port 2(vethb1ae047) entered blocking state 2 月 13 09:32:56 xx kernel: docker0: port 2(vethb1ae047) entered disabled state 2 月 13 09:32:56 xx kernel: device vethb1ae047 entered promiscuous mode 2 月 13 09:32:56 xx NetworkManager[1263]: <info> [1613179976.7431] manager: (vethaeeaa88): new Veth device (/org/freedesktop/NetworkManager/Devices/1410) 2 月 13 09:32:56 xx NetworkManager[1263]: <info> [1613179976.7441] manager: (vethb1ae047): new Veth device (/org/freedesktop/NetworkManager/Devices/1411) 2 月 13 09:32:56 xx systemd-udevd[1872858]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable. 2 月 13 09:32:56 xx systemd-udevd[1872856]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable. 2 月 13 09:32:56 xx systemd-udevd[1872858]: Using default interface naming scheme 'v245'. 2 月 13 09:32:56 xx systemd-udevd[1872858]: vethaeeaa88: Could not generate persistent MAC: No data available 2 月 13 09:32:56 xx systemd-udevd[1872856]: Using default interface naming scheme 'v245'. 2 月 13 09:32:56 xx systemd-udevd[1872856]: vethb1ae047: Could not generate persistent MAC: No data available 2 月 13 09:32:56 xx polkitd(authority=local)[1299]: Unregistered Authentication Agent for unix-process:1872840:63220323 (system bus name :1.875, object path /org/freedesktop/PolicyKit1/Authenticatio> 2 月 13 09:32:57 xx containerd[1546]: time="2021-02-13T09:32:57.325928526+08:00" level=info msg="shim containerd-shim started" address="unix:///run/containerd/s/9a0e9724da48b957eb2f02124421f9ff074d3> 2 月 13 09:32:57 xx systemd[3813]: run-docker-runtime\x2drunc-moby-675f25a5a8b9e86070480e8abe2ab1fe30a10505194f28b872b7ca0db0ff7ef8-runc.Kjcwfg.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:32:57 xx systemd[1]: run-docker-runtime\x2drunc-moby-675f25a5a8b9e86070480e8abe2ab1fe30a10505194f28b872b7ca0db0ff7ef8-runc.Kjcwfg.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit run-docker-runtime\x2drunc-moby-675f25a5a8b9e86070480e8abe2ab1fe30a10505194f28b872b7ca0db0ff7ef8-runc.Kjcwfg.mount has successfully entered the 'dead' state. 2 月 13 09:32:57 xx kernel: eth0: renamed from vethaeeaa88 2 月 13 09:32:57 xx kernel: IPv6: ADDRCONF(NETDEV_CHANGE): vethb1ae047: link becomes ready 2 月 13 09:32:57 xx kernel: docker0: port 2(vethb1ae047) entered blocking state 2 月 13 09:32:57 xx kernel: docker0: port 2(vethb1ae047) entered forwarding state 2 月 13 09:32:57 xx NetworkManager[1263]: <info> [1613179977.5736] device (vethb1ae047): carrier: link connected 2 月 13 09:32:57 xx gnome-shell[4090]: Removing a network device that was not added 2 月 13 09:32:57 xx gnome-shell[2375]: Removing a network device that was not added 2 月 13 09:32:57 xx docker[1872880]: blog_mysql 2 月 13 09:32:59 xx avahi-daemon[1256]: Joining mDNS multicast group on interface vethb1ae047.IPv6 with address fe80::bd:24ff:fe31:6cd6. 2 月 13 09:32:59 xx avahi-daemon[1256]: New relevant interface vethb1ae047.IPv6 for mDNS. 2 月 13 09:32:59 xx avahi-daemon[1256]: Registering new address record for fe80::bd:24ff:fe31:6cd6 on vethb1ae047.*. 2 月 13 09:33:07 xx dockerd[1576671]: time="2021-02-13T09:33:07.928906586+08:00" level=info msg="Container 675f25a5a8b9e86070480e8abe2ab1fe30a10505194f28b872b7ca0db0ff7ef8 failed to exit within 10 s> 2 月 13 09:33:08 xx containerd[1546]: time="2021-02-13T09:33:08.184891024+08:00" level=info msg="shim reaped" id=675f25a5a8b9e86070480e8abe2ab1fe30a10505194f28b872b7ca0db0ff7ef8 2 月 13 09:33:08 xx dockerd[1576671]: time="2021-02-13T09:33:08.195821548+08:00" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete" 2 月 13 09:33:08 xx kernel: docker0: port 2(vethb1ae047) entered disabled state 2 月 13 09:33:08 xx kernel: vethaeeaa88: renamed from eth0 2 月 13 09:33:08 xx NetworkManager[1263]: <info> [1613179988.3165] manager: (vethaeeaa88): new Veth device (/org/freedesktop/NetworkManager/Devices/1412) 2 月 13 09:33:08 xx systemd-udevd[1873154]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable. 2 月 13 09:33:08 xx systemd-udevd[1873154]: Using default interface naming scheme 'v245'. 2 月 13 09:33:08 xx systemd-udevd[1873154]: ethtool: autonegotiation is unset or enabled, the speed and duplex are not writable. 2 月 13 09:33:08 xx avahi-daemon[1256]: Interface vethb1ae047.IPv6 no longer relevant for mDNS. 2 月 13 09:33:08 xx kernel: docker0: port 2(vethb1ae047) entered disabled state 2 月 13 09:33:08 xx avahi-daemon[1256]: Leaving mDNS multicast group on interface vethb1ae047.IPv6 with address fe80::bd:24ff:fe31:6cd6. 2 月 13 09:33:08 xx kernel: device vethb1ae047 left promiscuous mode 2 月 13 09:33:08 xx kernel: docker0: port 2(vethb1ae047) entered disabled state 2 月 13 09:33:08 xx avahi-daemon[1256]: Withdrawing address record for fe80::bd:24ff:fe31:6cd6 on vethb1ae047. 2 月 13 09:33:08 xx NetworkManager[1263]: <info> [1613179988.4405] device (vethb1ae047): released from master device docker0 2 月 13 09:33:08 xx gnome-shell[2375]: Removing a network device that was not added 2 月 13 09:33:08 xx gnome-shell[4090]: Removing a network device that was not added 2 月 13 09:33:08 xx gnome-shell[4090]: Removing a network device that was not added 2 月 13 09:33:08 xx gnome-shell[2375]: Removing a network device that was not added 2 月 13 09:33:08 xx systemd[1659]: run-docker-netns-b3796211c9c6.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:33:08 xx systemd[3813]: run-docker-netns-b3796211c9c6.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:33:08 xx systemd[1]: run-docker-netns-b3796211c9c6.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit run-docker-netns-b3796211c9c6.mount has successfully entered the 'dead' state. 2 月 13 09:33:08 xx systemd[3813]: var-lib-docker-overlay2-c44d12767b3df5f7d341d9363a331d824baae317460677d8d1a635ddfb6b70e3-merged.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:33:08 xx systemd[1659]: var-lib-docker-overlay2-c44d12767b3df5f7d341d9363a331d824baae317460677d8d1a635ddfb6b70e3-merged.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit UNIT has successfully entered the 'dead' state. 2 月 13 09:33:08 xx systemd[1]: var-lib-docker-overlay2-c44d12767b3df5f7d341d9363a331d824baae317460677d8d1a635ddfb6b70e3-merged.mount: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit var-lib-docker-overlay2-c44d12767b3df5f7d341d9363a331d824baae317460677d8d1a635ddfb6b70e3-merged.mount has successfully entered the 'dead' state. 2 月 13 09:33:09 xx docker[1872988]: blog_mysql 2 月 13 09:33:09 xx systemd[1]: blog_mysql.service: Succeeded. -- Subject: Unit succeeded -- Defined-By: systemd -- Support: -- -- The unit blog_mysql.service has successfully entered the 'dead' state. |
16
Meltdown OP systemctl status blog_mysql.service
● blog_mysql.service - docker blog mysql Loaded: loaded (/etc/systemd/system/blog_mysql.service; enabled; vendor preset: enabled) Active: inactive (dead) since Sat 2021-02-13 09:33:09 CST; 52min ago Main PID: 1872880 (code=exited, status=0/SUCCESS) 2 月 13 09:32:56 xx systemd[1]: Started docker blog mysql. 2 月 13 09:32:57 xx docker[1872880]: blog_mysql 2 月 13 09:33:09 xx docker[1872988]: blog_mysql 2 月 13 09:33:09 xx systemd[1]: blog_mysql.service: Succeeded. |
17
Jirajine 2021-02-13 12:07:16 +08:00 via Android
docker 有自己的 daemon 进程 dockerd,所有容器都是由该进程管理,你只需要用 systemd 启动 dockerd 就可以了。
当然你也可以换用 daemonless 的 podman,所有容器都由系统服务管理。 前者的好处是跨平台,无论你的 init 是 systemd 还是 openrc 之类的,都不需要额外修改启动脚本。而后者则使用了系统原生的服务管理,应该是你想要的。 |
18
SenLief 2021-02-13 12:46:51 +08:00 via Android
docker 有自己的守护进程,没必要用 systemd 守护。之前看了一篇文章举例的,就想 docker 容器中的 nginx 运行不是用 systemd 而是直接 nginx 。
|
19
Meltdown OP @Kobayashi 改成 Type=oneshot 和 RemainAfterExit=true 是可以的,Type=simple 和 Type=forking 都不行,容器退出应该是 systemd 有个超时检测调用了 ExecStop
|
20
dzdh 2021-03-28 22:45:00 +08:00
systemd 的话把 Type=simple 改成 notify 或者 oneshot 试试看
|