Skip to main content

generate init configuration file (systemd/Upstart) from docker-compose.yml

Project description

The dc2service utility generates configuration files for systemd/Upstart based on a single docker-compose YAML input file.

The init type is autodetected, but can be overruled from the commandline.

The service name is determinted by the container_name entries for all the services in the YAML file. Each service has to have a container_name specified.

As docker-compose YAML file format doesn’t allow for extra metadata fields, two comment entries at the beginning of the YAML file are parsed for a description and author.

If external ports are specified (i.e. of the form “ip:ip”) then the external port numbers are extended to the description.

The file generation is template based and can easily be adjusted to your needs. Their location can be viewed by doing dc2service templates

Example input YAML file for Mongo DB

docker-compose.yml file for single service running Mongo DB with an external (i.e. host oriented) port:

# author: Anthon van der Neut <a.van.der.neut@ruamel.eu>
# description: mongo container
mongodb:
  container_name: mongo
  image: mongo:2.4
  volumes:
   - /data1/DB/mongo:/data/db
  ports:
  - 27017:27017

systemd

The command dc2service --systemd generate /opt/docker/mongo/docker-compose.yml will generate the file /etc/systemd/system/mongo-docker.service:

[Unit]
Description=mongo container on port 27017
# Author = Anthon van der Neut <a.van.der.neut@ruamel.eu> (dc2service 0.1.0.dev)
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/opt/util/docker-compose/bin/docker-compose -f /opt/docker/mongo/docker-compose.yml up -d --no-recreate
ExecStop=/opt/util/docker-compose/bin/docker-compose -f /opt/docker/mongo/docker-compose.yml stop

[Install]
WantedBy=multi-user.target

Upstart

The command dc2service --upstart generate /opt/docker/mongo/docker-compose.yml will generate the file /etc/init/mongo-docker.conf:

description "mongo container on port 27017"
author "Anthon van der Neut <a.van.der.neut@ruamel.eu> (dc2service 0.1.0.dev)"
start on filesystem and started docker
stop on runlevel [!2345]
respawn

pre-start script
  /opt/util/docker-compose/bin/docker-compose -f /opt/docker/mongo/docker-compose.yml up -d --no-recreate
end script

script
  sleepWhileAppIsUp(){
    while docker ps --filter=name=mongo | grep -qF mongo ; do
      sleep 2
    done
  }

  sleepWhileAppIsUp
end script

post-stop script
  if docker ps --filter=name=mongo | grep -qF mongo; then
    /opt/util/docker-compose/bin/docker-compose -f /opt/docker/mongo/docker-compose.yml stop
  fi
end script

Project details


Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page