简体   繁体   English

如何在使用 systemd 退出 docker-machine 之前运行 docker 命令?

[英]How to run the docker commands before exiting docker-machine with systemd?

I run my containers on system boot with this systemd config:我使用这个 systemd 配置在系统启动时运行我的容器:

[Unit]
After=docker.service
Requires=docker.service

[Service]
User=sterzhakov
Group=sterzhakov
ExecStart=/home/sterzhakov/conatiners-start.sh
ExecStop=/home/sterzhakov/containers-stop.sh
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

and everything starts great一切都开始美好

but on reboot I need to run another script to clean up volumes, containers inside my docker-machine但是在重新启动时,我需要运行另一个脚本来清理我的 docker-machine 中的卷和容器

But this does not happen, here is the log of this script:但这不会发生,这里是这个脚本的日志:

2021-02-07 02:35:09 | start VTB stop
Error checking TLS connection: Host is not running
Removing network docker_default
Network docker_default not found.
Removing network docker_default
Network docker_default not found

How to properly run the script inside the docker-machine before reboot |重启前如何在 docker-machine 中正确运行脚本 | shutdown?关闭?

This is difficult because you cannot guarantee a clean shutdown.这很困难,因为您无法保证完全关闭。 Machines freeze and crash sometimes.机器有时会冻结和崩溃。 You're better off cleaning up at startup.你最好在启动时清理。

You can create another systemd-service to clean up and place a dependency on it from the service that starts your containers.您可以创建另一个 systemd-service 来清理并从启动容器的服务中放置对它的依赖。

Or, it may be better to simply put a 'restart always' policy on your containers and docker will just restart them for you automatically when the system reboots.或者,最好在您的容器上简单地设置一个“始终重启”策略,docker 会在系统重启时自动为您重启它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM