Docker Community Forums

Share and learn in the Docker community.

Running docker-compose exec commands in systemctl fails

docker

#1

I am running a graphite with this docker-compose.yml file

version: '2'
services:
  graphite:
    image: graphiteapp/graphite-statsd
    container_name: graphite
    restart: always
    volumes:
      - ./conf:/opt/graphite/conf
      - ./storage:/opt/graphite/storage
    environment:
      RELAY: 1
      GRAPHITE_CARBONLINK_HOSTS: "127.0.0.1:7002:a, 127.0.0.1:7102:b"
    ports:
      - "80:80"
      - "81:81"
      - "2003-2004:2003-2004"
      - "2103-2104:2103-2104"
      - "2023-2024:2023-2024"
      - "8125:8125/udp"
      - "8126:8126"

I would like to create a service which would be run with systemctl start graphite.service command.
It supposed to start graphite and run two carbon caches instances but it fails on those two commands:

ExecStartPost=/usr/bin/docker-compose exec graphite sh -c "source /opt/graphite/bin/activate && python3 /opt/graphite/bin/carbon-cache.py --instance=b start" 

ExecStartPost=/usr/bin/docker-compose exec graphite sh -c "source /opt/graphite/bin/activate && python3 /opt/graphite/bin/carbon-cache.py --instance=c start"

Here is a graphite.service file:

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

    [Service]
    WorkingDirectory=/opt/docker-apps/graphite
    Restart=always

    ExecStartPre=-/usr/bin/rm -f storage/carbon-cache-b.pid
    ExecStartPre=-/usr/bin/rm -f storage/carbon-cache-c.pid

    ExecStart=/usr/bin/docker-compose up -d
    ExecStartPost=/usr/bin/docker-compose exec graphite sh -c "source /opt/graphite/bin/activate && python3 /opt/graphite/bin/carbon-cache.py --instance=b start"
    ExecStartPost=/usr/bin/docker-compose exec graphite sh -c "source /opt/graphite/bin/activate && python3 /opt/graphite/bin/carbon-cache.py --instance=c start"

    ExecStop=/usr/bin/docker-compose stop

    [Install]
    WantedBy=multi-user.target

Here is some logs:

[vagrant@localhost graphite]$ systemctl status graphite.service
● graphite.service - Graphite service
   Loaded: loaded (/etc/systemd/system/graphite.service; enabled; vendor preset: disabled)
   Active: deactivating (stop) (Result: exit-code) since Mon 2019-02-11 16:21:06 UTC; 17min ago
  Process: 5498 ExecStartPost=/usr/bin/docker-compose exec graphite sh -c source /opt/graphite/bin/activate && python3 /opt/graphite/bin/carbon-cache.py --instance=b start (code=exited, status=1/FAILURE)
  Process: 5497 ExecStart=/usr/bin/docker-compose up -d (code=exited, status=0/SUCCESS)
  Process: 5495 ExecStartPre=/usr/bin/rm -f storage/carbon-cache-c.pid (code=exited, status=0/SUCCESS)
  Process: 5494 ExecStartPre=/usr/bin/rm -f storage/carbon-cache-b.pid (code=exited, status=0/SUCCESS)
 Main PID: 5497 (code=exited, status=0/SUCCESS);         : 5517 (docker-compose)
    Tasks: 3
   Memory: 45.5M
   CGroup: /system.slice/graphite.service
           └─control
             ├─5517 /usr/bin/docker-compose stop
             └─5564 /usr/bin/docker-compose stop

Any idea what I am doing wrong?