Linux: systemd сервис для Docker Compose

Автор: | 14/08/2018
 

Есть Docker Compose файл, который запускает Prometheus стек.

Рабочий каталог Prometheus – /opt/prometheus, в котором находится Compose файл prometheus-compose.yml.

Для управления им через systemd – создаём файл /etc/systemd/system/prometheus.service:

[Unit]
Description=Prometheus stack
Requires=docker.service
After=docker.service

[Service]
Restart=always
WorkingDirectory=/opt/prometheus

ExecStart=/usr/local/bin/docker-compose -f prometheus-compose.yml up

ExecStop=/usr/local/bin/docker-compose -f prometheus-compose.yml down

[Install]
WantedBy=multi-user.target

Проверяем.

Запускаем:

[simterm]

# systemctl start prometheus.service

[/simterm]

Проверяем контейнер:

[simterm]

# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                  PORTS                    NAMES
da348d05d494        prom/prometheus:v1.8.2   "/bin/prometheus -co…"   1 second ago        Up Less than a second   0.0.0.0:9090->9090/tcp   prometheus_prometheus-server_1

[/simterm]

Статус процесса:

[simterm]

# systemctl status prometheus.service
● prometheus.service - Prometheus stack
   Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-06-13 16:43:56 EEST; 38s ago
 Main PID: 9164 (docker-compose)
    Tasks: 3 (limit: 4915)
   Memory: 29.1M
      CPU: 392ms
   CGroup: /system.slice/prometheus.service
           └─9164 /usr/bin/python /usr/local/bin/docker-compose -f prometheus-compose.yml up

Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: [105B blob data]
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Starting prometheus (version=1.8.2, branch=HEAD, revision=5211b96d4d1291c3dd1a569f711d3b301b635ecb)" source="
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Build context (go=go1.9.2, user=root@1412e937e4ad, date=20171104-16:09:14)" source="main.go:88"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Host details (Linux 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64 da348d05d494 (none))" source="ma
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Loading configuration file /etc/prometheus.yml" source="main.go:254"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Listening on :9090" source="web.go:341"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Loading series map and head chunks..." source="storage.go:428"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="582 series loaded." source="storage.go:439"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Server is Ready to receive requests." source="main.go:230"
Jun 13 16:43:57 monitonrig-dev docker-compose[9164]: prometheus-server_1  | time="2018-06-13T13:43:57Z" level=info msg="Starting target manager..." source="targetmanager.go:63"

[/simterm]

При именениях в файле сервиса – не забываем сделать reload:

[simterm]

# systemctl daemon-reload

[/simterm]

Готово.