Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Created September 4, 2017 01:51
Show Gist options
  • Save anxiousmodernman/be7988fd54c295c01a21579520feccc8 to your computer and use it in GitHub Desktop.
Save anxiousmodernman/be7988fd54c295c01a21579520feccc8 to your computer and use it in GitHub Desktop.
Systemd
{% if ports is undefined %} {% set ports=[] %} {% endif %}
{% if env_vars is undefined %} {% set env_vars=[] %} {% endif %}
{% if volume_mounts is undefined %} {% set volume_mounts=[] %} {% endif %}
{% if hostname is undefined %} {% set host_arg='--net=host' %} {% else %} {% set host_arg='--hostname=%s' % hostname %} {% endif %}
{% if image_cmd is undefined %} {% set image_cmd='' %} {% endif %}
[Unit]
Description={{ description }}
Requires=network-online.target docker.service
After=network-online.target
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill {{ service_name }}
ExecStartPre=-/usr/bin/docker rm {{ service_name }}
ExecStartPre=/usr/bin/docker pull {{ image_name }}{% if image_tag %}:{{ image_tag }}{% endif %}
ExecStart=/usr/bin/docker run --name {{ service_name }} {{ host_arg }} {% for mount in volume_mounts %} {{ '-v %s' % mount }} {% endfor %} {% for port in ports %} {{ '-p %s' % port }} {% endfor %} {% for var in env_vars if env_vars is defined %} {{ '-e %s' % var }} {% endfor %} {{ image_name }}{{ ':%s' % image_tag if image_tag else ''}} {{ image_cmd if image_cmd else ''}}
ExecStop=/usr/bin/docker stop {{ service_name }}
RestartSec=5
Restart=on-failure
StartLimitInterval=600
StartLimitBurst=5
StartLimitAction=none
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment