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

时间:2018-08-15 03:16:56

标签: docker-compose systemd rhel7

我正在尝试作为RHEL 7上的systemd服务启动docker-compose项目。这是我的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并重新加载服务),则容器在运行后将被删除。 status命令的输出为:

   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服务在ExecStop脚本之后立即执行ExecStart脚本。可能是什么原因?

1 个答案:

答案 0 :(得分:2)

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

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

尝试不带-d选项运行它。

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

请参阅TypeExecStop的官方文档。