17 Ekim 2018 Çarşamba

How to run docker-compose script as system service on Debian

You can follow steps in order to make docker-compose script system service and controlled by systemctl on debian linux.

Prerequisites: docker, docker-compose must be installed


  1. Get your docker-compose path with command which
  2. # which docker-compose
    /usr/bin/docker-compose
    


  3. create file /etc/systemd/system/docker-compose-app.service
  4. # vi /etc/systemd/system/docker-compose-app.service
    
    [Unit]
    Description=Docker Compose Application Service
    Requires=docker.service
    After=docker.service
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    #folder where your docker-compose.yml resides
    WorkingDirectory=/data/docker 
    #dont forget to change docker-compose path if you see different path in step 1
    ExecStart=/usr/bin/docker-compose up -d  
    ExecStop=/usr/bin/docker-compose down
    TimeoutStartSec=0
    
    [Install]
    WantedBy=multi-user.target
    
    


  5. check status of containers and ensure all stopped
  6. # docker ps -a
    CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                       PORTS               NAMES
    5b5681a7cee8        graylog/graylog:2.4                                    "/docker-entrypoint.…"   6 minutes ago       Exited (143) 4 minutes ago                       docker_graylog_1
    2fe983b90dd6        mongo:3                                                "docker-entrypoint.s…"   7 minutes ago       Exited (0) 4 minutes ago                         docker_mongo_1
    1ec052b38c8f        docker.elastic.co/elasticsearch/elasticsearch:5.6.12   "/bin/bash bin/es-do…"   7 minutes ago       Exited (143) 4 minutes ago                       docker_elasticsearch_1
    


  7. start service and check status
  8. root@ekampuslog:/home/mergen# systemctl start docker-compose-app
    root@ekampuslog:/home/mergen# systemctl status docker-compose-app
    ● docker-compose-app.service - Docker Compose Application Service
       Loaded: loaded (/etc/systemd/system/docker-compose-app.service; enabled; vendor preset: enabled)
       Active: active (exited) since Wed 2018-10-17 13:30:47 +03; 1min 18s ago
      Process: 839 ExecStart=/usr/bin/docker-compose up -d (code=exited, status=0/SUCCESS)
     Main PID: 839 (code=exited, status=0/SUCCESS)
          CPU: 1.189s
    
    Oct 17 13:30:39 ekampuslog systemd[1]: Starting Docker Compose Application Service...
    Oct 17 13:30:43 ekampuslog docker-compose[839]: Starting docker_elasticsearch_1
    Oct 17 13:30:43 ekampuslog docker-compose[839]: Starting docker_mongo_1
    Oct 17 13:30:45 ekampuslog docker-compose[839]: Starting docker_graylog_1
    Oct 17 13:30:47 ekampuslog systemd[1]: Started Docker Compose Application Service.
    
    
    # docker ps -a
    CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                    PORTS                                                                                                                    NAMES
    5b5681a7cee8        graylog/graylog:2.4                                    "/docker-entrypoint.…"   20 minutes ago      Up 11 minutes (healthy)   0.0.0.0:514->514/tcp, 0.0.0.0:9000->9000/tcp, 0.0.0.0:514->514/udp, 0.0.0.0:12201->12201/tcp, 0.0.0.0:12201->12201/udp   docker_graylog_1
    2fe983b90dd6        mongo:3                                                "docker-entrypoint.s…"   21 minutes ago      Up 11 minutes             27017/tcp                                                                                                                docker_mongo_1
    1ec052b38c8f        docker.elastic.co/elasticsearch/elasticsearch:5.6.12   "/bin/bash bin/es-do…"   21 minutes ago      Up 11 minutes             9200/tcp, 9300/tcp                                                                                                       docker_elasticsearch_1
    
    


  9. Enable service to auto startup
  10. # systemctl enable docker-compose-app
    Created symlink /etc/systemd/system/multi-user.target.wants/docker-compose-app.service → /etc/systemd/system/docker-compose-app.service.
    


  11. You can use start and stop commands
  12. # systemctl start docker-compose-app
    # systemctl stop docker-compose-app
    

Hiç yorum yok:

Yorum Gönder