Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally support removing container before trying to start it #238

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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