简体   繁体   English

如何“重置” docker-compose systemd 服务?

[英]How to “reset” a docker-compose systemd service?

I have created a systemd service that starts a set of Docker containers using Docker-Compose, as outlined in this answer :我创建了一个systemd服务,它使用 Docker-Compose 启动一组 Docker 容器,如本答案所述:

# /etc/systemd/system/docker-compose-app.service

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

This allows me to start the Docker-Compose services using这允许我使用 Docker-Compose 服务启动

sudo systemctl start docker-compose-app

which uses docker-compose up -d under the hood, and shutting them down using它使用docker-compose up -d在引擎盖下,并使用关闭它们

sudo systemctl stop docker-compose-app

which uses docker-compose down .它使用docker-compose down Please note that the down command is run without the -v flag, which means that volumes will remain in place, preserving the data of my containers across restarts/recreation.请注意, down命令在没有-v标志的情况下运行,这意味着卷将保持原位,在重新启动/重新创建时保留我的容器的数据。 This is pretty much what I want in the majority of cases.在大多数情况下,这几乎是我想要的。

There are situations where I want to erase all data in the services, basically running the down -v command instead of just down .在某些情况下,我想擦除服务中的所有数据,基本上是运行down -v命令而不是仅仅运行down

Is there a way to extend the above systemd service definition to allow for an additional command (or using one of the existing systemctl commands) that would allow me to run the occasional down -v if needed.有没有办法扩展上述systemd服务定义以允许附加命令(或使用现有的systemctl命令之一),如果需要,我可以偶尔运行down -v I want to do this ad-hoc if needed, not scheduled or anything like that.如果需要,我想临时做这个,而不是计划或类似的。

How can I run我怎么能跑

docker-compose down -v

occasionally if needed through the same systemd setup, while keeping the standard functionality of maintaining the containers' data across restarts?偶尔如果需要通过相同的systemd设置,同时保持跨重启维护容器数据的标准功能?

You may try to use ExecReload definition:您可以尝试使用ExecReload定义:

ExecReload=/usr/local/bin/docker-compose down -v && /usr/local/bin/docker-compose up -d

And then you can use:然后你可以使用:

sudo systemctl reload docker-compose-app

So "reload" command will be used for "reset" in this case.因此,在这种情况下,“reload”命令将用于“reset”。

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

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