Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic Fleet Unit service using data from etcd as environment variables to docker containers
[Unit]
Description=My application
# Requirements
Requires=etcd2.service
Requires=docker.service
# Dependency ordering
After=etcd2.service
After=docker.service
[Service]
# Let processes take awhile to start up (for first run Docker containers)
TimeoutStartSec=0
# Change killmode from "control-group" to "none" to let Docker remove
# work correctly.
KillMode=none
# Run as user core, which have the necessary credentials for pulling from
# private registry
User=core
# Environment variables to DRY up config
Environment=APPLICATION=my-app
Environment=IMAGE=user/my-app
# Pre-start and Start
## Directives with "=-" are allowed to fail without consequence
ExecStartPre=-/usr/bin/docker kill $APPLICATION
ExecStartPre=-/usr/bin/docker rm $APPLICATION
ExecStartPre=/usr/bin/docker pull $IMAGE
## The run command gets all config variables from etcd and construct the
## docker run command passing all variables
ExecStart=/bin/sh -c '/usr/bin/docker run --name $APPLICATION \
$(for e in $(etcdctl ls /daemons/config/$APPLICATION); do \
echo "-e $(basename $e)=$(etcdctl get $e) "; \
done) \
$IMAGE'
# Stop
ExecStop=/usr/bin/docker stop $APPLICATION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.