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

systemd: seems like ExecStop script is executed immediately after the start command is run

我正在尝试在 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

一切正常 - 容器 运行 并保持 运行ning。这是 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-在分离模式下撰写(选项 -d)。启动容器后,docker-compose 将守护容器并退出。

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

尝试 运行 不带 -d 选项。

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

参见 Type and ExecStop 的官方文档。