From cc10d97ee0a8f85f8d94f6ec4b1918c906338afd Mon Sep 17 00:00:00 2001 From: Chad Bean Date: Thu, 16 Jan 2020 09:20:29 -0500 Subject: [PATCH] feat: support optional container removal before start in upstart/systemd --- docker/files/service_file.jinja | 3 ++- docker/files/systemd.conf | 4 ++++ docker/files/upstart.conf | 7 +++++++ pillar.example | 4 ++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docker/files/service_file.jinja b/docker/files/service_file.jinja index cc5b8136..c30c0318 100644 --- a/docker/files/service_file.jinja +++ b/docker/files/service_file.jinja @@ -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) %} @@ -16,4 +17,4 @@ {%- include 'docker/files/systemd.conf' %} {%- elif grains['init'] == 'upstart' %} {%- include 'docker/files/upstart.conf' %} -{%- endif %} \ No newline at end of file +{%- endif %} diff --git a/docker/files/systemd.conf b/docker/files/systemd.conf index 7ee89449..587ebae1 100644 --- a/docker/files/systemd.conf +++ b/docker/files/systemd.conf @@ -5,6 +5,7 @@ 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 @@ -12,6 +13,9 @@ Restart=always 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 %} diff --git a/docker/files/upstart.conf b/docker/files/upstart.conf index a5da4474..a879ab44 100644 --- a/docker/files/upstart.conf +++ b/docker/files/upstart.conf @@ -11,6 +11,7 @@ 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 @@ -18,6 +19,12 @@ pre-start script 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 diff --git a/pillar.example b/pillar.example index 6dd30c2b..a26b48d1 100644 --- a/pillar.example +++ b/pillar.example @@ -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: @@ -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: