gpt4 book ai didi

docker-compose - systemd:似乎 ExecStop 脚本在启动命令运行后立即执行

转载 作者:行者123 更新时间:2023-12-02 20:18:51 27 4
gpt4 key购买 nike

我正在尝试在 RHEL 7 上启动一个 docker-compose 项目作为 systemd 服务。这是我的 systemd 脚本 (/etc/systemd/system/wp.service):

[Unit]
Description=wp service with docker compose
Requires=docker.service
After=docker.service

[Service]
EnvironmentFile=/home/ec2-user/projects/wp/project-dir/vars.env
WorkingDirectory=/home/ec2-user/projects/wp/project-dir

# ExecStartPre=/usr/bin/docker-compose down

ExecStart=/usr/bin/docker-compose up -d --build --remove-orphans

# ExecStop=/usr/bin/docker-compose down

[Install]
WantedBy=multi-user.target

当我执行以下命令时:

sudo systemctl status wp.service

一切正常 - 容器运行并保持运行。这是 sudo systemctl status wp.service

的输出
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: ---> Using cache
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: ---> 7392974149d3
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: Successfully built 7392974149d3
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: Successfully tagged foo_wp:latest
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: Creating mysql ...
Aug 15 03:07:22 ip-172-31-33-87.ec2.internal docker-compose[4185]: [55B blob data]
Aug 15 03:07:23 ip-172-31-33-87.ec2.internal docker-compose[4185]: [37B blob data]

容器已启动:

[ec2-user@ip-172-31-33-87 ~]$ sudo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
579b52c8e3bc foo_wp "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:9101->80/tcp wp
3c418cfe2b9c mariadb:10.3.8-bionic "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp mysql

但是,如果我取消注释上面的 ExecStop 行(并运行 docker-compos down 并重新加载服务),则容器在运行后将被删除。状态命令的输出为:

   Loaded: loaded (/etc/systemd/system/wp.service; disabled; vendor preset: disabled)
Active: deactivating (stop) since Wed 2018-08-15 03:12:12 UTC; 7s ago
Process: 4862 ExecStart=/usr/bin/docker-compose up -d --build --remove-orphans (code=exited, status=0/SUCCESS)
Main PID: 4862 (code=exited, status=0/SUCCESS); : 5165 (docker-compose)
Tasks: 2
Memory: 19.0M
CGroup: /system.slice/wp.service
└─control
└─5165 /usr/bin/python2 /usr/bin/docker-compose down

Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: Step 3/3 : COPY wordpress/ /var/www/html/
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: ---> Using cache
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: ---> 7392974149d3
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: Successfully built 7392974149d3
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: Successfully tagged foo_wp:latest
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: Creating mysql ...
Aug 15 03:12:11 ip-172-31-33-87.ec2.internal docker-compose[4862]: [55B blob data]
Aug 15 03:12:12 ip-172-31-33-87.ec2.internal docker-compose[4862]: [37B blob data]
Aug 15 03:12:12 ip-172-31-33-87.ec2.internal docker-compose[5165]: Stopping wp ...
Aug 15 03:12:12 ip-172-31-33-87.ec2.internal docker-compose[5165]: Stopping mysql ...

容器已被移除:

sudo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[ec2-user@ip-172-31-33-87 foo]$

似乎 systemd 服务在 ExecStart 脚本之后立即执行 ExecStop 脚本。可能是什么原因?

最佳答案

您正在分离模式下运行 docker-compose(选项 -d)。启动容器后,docker-compose 将守护容器并退出。

Systemd 监视 docker-compose 的 PID,当它退出时,假设您的程序已停止并将调用 ExecStop 命令。

尝试在不使用 -d 选项的情况下运行它。

systemd 这样做的原因是因为您没有指定单元的类型,并且默认情况下它会恢复为 Type=simple

请参阅 Type 的官方文档和 ExecStop .

关于docker-compose - systemd:似乎 ExecStop 脚本在启动命令运行后立即执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com