Skip to content

Commit

Permalink
feat: support optional container removal before start in upstart/systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
solarchad committed Jan 22, 2020
1 parent 2590d61 commit cc10d97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docker/files/service_file.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{%- set stopoptions = stopoptions|join(' ') %}
{%- set args = args|join(' ') %}

{%- set docker_prestart_remove_command = "rm -f %s"|format(name) %}
{%- set docker_start_command = "run %s --name=%s %s %s %s"|format(runoptions, name, container.image, cmd, args) %}
{%- set docker_stop_command = "stop %s %s"|format(stopoptions, name) %}
{%- set docker_poststop_command = "rm -f %s"|format(name) %}
Expand All @@ -16,4 +17,4 @@
{%- include 'docker/files/systemd.conf' %}
{%- elif grains['init'] == 'upstart' %}
{%- include 'docker/files/upstart.conf' %}
{%- endif %}
{%- endif %}
4 changes: 4 additions & 0 deletions docker/files/systemd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ After=docker.service

{%- set remove_on_stop = container.get("remove_on_stop", False) %}
{%- set pull_before_start = container.get("pull_before_start") or False %}
{%- set remove_before_start = container.get("remove_before_start") or False %}

[Service]
Restart=always
{%- if pull_before_start %}
ExecStartPre=/usr/bin/docker pull {{ container.image }}
{%- endif %}

{%- if remove_before_start %}
ExecStartPre=-/usr/bin/docker {{ docker_prestart_remove_command }}
{%- endif %}
ExecStart=/usr/bin/docker {{ docker_start_command }}
ExecStop=/usr/bin/docker {{ docker_stop_command }}
{%- if remove_on_stop %}
Expand Down
7 changes: 7 additions & 0 deletions docker/files/upstart.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ respawn
{%- set cmd = container.get("cmd", "") %}

{%- set pull_before_start = container.get("pull_before_start") or False %}
{%- set remove_before_start = container.get("remove_before_start") or False %}

{%- if pull_before_start %}
pre-start script
/usr/bin/docker pull {{ container.image }}
end script
{%- endif %}

{%- if remove_before_start %}
pre-start script
/usr/bin/docker rm -f {{ name }}
end script
{%- endif %}

script
exec docker {{ docker_start_command }}
end script
Expand Down
4 changes: 4 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ docker-containers:
# Pull image on service restart
# (useful if you override the same tag. example: latest)
pull_before_start: true
# Remove container on service start
remove_before_start: false
# Do not force container removal on stop (unless true)
remove_on_stop: false
runoptions:
Expand All @@ -38,6 +40,8 @@ docker-containers:
# Pull image on service restart
# (useful if you override the same tag. example: latest)
pull_before_start: true
# Remove container on service start
remove_before_start: false
# Do not force container removal on stop (unless true)
remove_on_stop: false
runoptions:
Expand Down

0 comments on commit cc10d97

Please sign in to comment.