V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Meltdown
V2EX  ›  Docker

ubuntu20.04 用 systemd 运行容器会自动异常退出

  •  
  •   Meltdown · 2021-02-12 16:11:47 +08:00 · 2500 次点击
    这是一个创建于 1380 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如 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
    
    20 条回复    2021-03-28 22:45:00 +08:00
    kaneg
        1
    kaneg  
       2021-02-12 16:30:43 +08:00 via iPhone
    为什么要用 systemd 来启动 docker 呢?如果你想系统启动的时候自动启动 docker 容器,docker 有自己的参数,restart-policy 为 always 即可
    Meltdown
        2
    Meltdown  
    OP
       2021-02-12 17:02:51 +08:00
    @kaneg 是为了开机启动的,因为不知道有 restart-policy 这个参数。。。感谢!
    boris93
        3
    boris93  
       2021-02-12 18:13:40 +08:00 via Android
    或者用 docker-compose 管理也行,配置文件写个 restart: always 就行
    Meltdown
        4
    Meltdown  
    OP
       2021-02-12 19:45:25 +08:00
    @boris93 开机启动的目的已经达到了,只是现在还奇怪为什么敲命令可以直接执行成功的,到 systemd 这里就不行了,看 dockerd 的日志也没看出什么名堂
    Kobayashi
        5
    Kobayashi  
       2021-02-12 20:23:03 +08:00 via Android
    因为 docker 就是这么工作的。容器就是进程,进程结束容器退出。是本完整的 docker 教程应该都有讲吧。

    https://yeasy.gitbook.io/docker_practice/image/dockerfile/cmd
    Kobayashi
        6
    Kobayashi  
       2021-02-12 20:36:03 +08:00 via Android
    理解错了,你是要 systemd 从外部控制容器启动。感觉情况差不多,systemd 也是要求被调用程序前台运行,或者你应该试试 systemd oneshot 。
    Meltdown
        7
    Meltdown  
    OP
       2021-02-12 23:50:40 +08:00
    @Kobayashi 我把启动容器的`-d`选项去掉就不会退出了
    mason961125
        8
    mason961125  
       2021-02-13 00:36:55 +08:00
    你至少也 systemctl status xx.service 或者 journalctl -u xx.service 发一下日志吧...
    mason961125
        9
    mason961125  
       2021-02-13 00:37:47 +08:00
    @Meltdown -d 是 detach 啊,这个时候 docker start 就退出了,容器进程交给 dockerd 管理,肯定达不到你让 systemd 管理的要求。
    ManjusakaL
        10
    ManjusakaL  
       2021-02-13 01:29:03 +08:00
    最起码发一下具体的日志吧。。。
    Meltdown
        11
    Meltdown  
    OP
       2021-02-13 09:37:06 +08:00
    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
    ```
    lululau
        12
    lululau  
       2021-02-13 09:46:20 +08:00 via iPhone
    systemctl start mysql/mariadb 不香吗
    Meltdown
        13
    Meltdown  
    OP
       2021-02-13 10:10:52 +08:00
    @lululau 通过反向代理做服务器用的,不想直接部署在物理机上
    Meltdown
        14
    Meltdown  
    OP
       2021-02-13 10:22:39 +08:00
    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”。
    Meltdown
        15
    Meltdown  
    OP
       2021-02-13 10:24:36 +08:00
    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.
    Meltdown
        16
    Meltdown  
    OP
       2021-02-13 10:26:18 +08:00
    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.
    Jirajine
        17
    Jirajine  
       2021-02-13 12:07:16 +08:00 via Android
    docker 有自己的 daemon 进程 dockerd,所有容器都是由该进程管理,你只需要用 systemd 启动 dockerd 就可以了。
    当然你也可以换用 daemonless 的 podman,所有容器都由系统服务管理。

    前者的好处是跨平台,无论你的 init 是 systemd 还是 openrc 之类的,都不需要额外修改启动脚本。而后者则使用了系统原生的服务管理,应该是你想要的。
    SenLief
        18
    SenLief  
       2021-02-13 12:46:51 +08:00 via Android
    docker 有自己的守护进程,没必要用 systemd 守护。之前看了一篇文章举例的,就想 docker 容器中的 nginx 运行不是用 systemd 而是直接 nginx 。
    Meltdown
        19
    Meltdown  
    OP
       2021-02-13 19:03:36 +08:00
    @Kobayashi 改成 Type=oneshot 和 RemainAfterExit=true 是可以的,Type=simple 和 Type=forking 都不行,容器退出应该是 systemd 有个超时检测调用了 ExecStop
    dzdh
        20
    dzdh  
       2021-03-28 22:45:00 +08:00
    systemd 的话把 Type=simple 改成 notify 或者 oneshot 试试看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1348 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 17:39 · PVG 01:39 · LAX 09:39 · JFK 12:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.