Skip to content

Commit cc10d97

Browse files
committed
feat: support optional container removal before start in upstart/systemd
1 parent 2590d61 commit cc10d97

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

docker/files/service_file.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{%- set stopoptions = stopoptions|join(' ') %}
99
{%- set args = args|join(' ') %}
1010

11+
{%- set docker_prestart_remove_command = "rm -f %s"|format(name) %}
1112
{%- set docker_start_command = "run %s --name=%s %s %s %s"|format(runoptions, name, container.image, cmd, args) %}
1213
{%- set docker_stop_command = "stop %s %s"|format(stopoptions, name) %}
1314
{%- set docker_poststop_command = "rm -f %s"|format(name) %}
@@ -16,4 +17,4 @@
1617
{%- include 'docker/files/systemd.conf' %}
1718
{%- elif grains['init'] == 'upstart' %}
1819
{%- include 'docker/files/upstart.conf' %}
19-
{%- endif %}
20+
{%- endif %}

docker/files/systemd.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ After=docker.service
55

66
{%- set remove_on_stop = container.get("remove_on_stop", False) %}
77
{%- set pull_before_start = container.get("pull_before_start") or False %}
8+
{%- set remove_before_start = container.get("remove_before_start") or False %}
89

910
[Service]
1011
Restart=always
1112
{%- if pull_before_start %}
1213
ExecStartPre=/usr/bin/docker pull {{ container.image }}
1314
{%- endif %}
1415

16+
{%- if remove_before_start %}
17+
ExecStartPre=-/usr/bin/docker {{ docker_prestart_remove_command }}
18+
{%- endif %}
1519
ExecStart=/usr/bin/docker {{ docker_start_command }}
1620
ExecStop=/usr/bin/docker {{ docker_stop_command }}
1721
{%- if remove_on_stop %}

docker/files/upstart.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ respawn
1111
{%- set cmd = container.get("cmd", "") %}
1212

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

1516
{%- if pull_before_start %}
1617
pre-start script
1718
/usr/bin/docker pull {{ container.image }}
1819
end script
1920
{%- endif %}
2021

22+
{%- if remove_before_start %}
23+
pre-start script
24+
/usr/bin/docker rm -f {{ name }}
25+
end script
26+
{%- endif %}
27+
2128
script
2229
exec docker {{ docker_start_command }}
2330
end script

pillar.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ docker-containers:
1414
# Pull image on service restart
1515
# (useful if you override the same tag. example: latest)
1616
pull_before_start: true
17+
# Remove container on service start
18+
remove_before_start: false
1719
# Do not force container removal on stop (unless true)
1820
remove_on_stop: false
1921
runoptions:
@@ -38,6 +40,8 @@ docker-containers:
3840
# Pull image on service restart
3941
# (useful if you override the same tag. example: latest)
4042
pull_before_start: true
43+
# Remove container on service start
44+
remove_before_start: false
4145
# Do not force container removal on stop (unless true)
4246
remove_on_stop: false
4347
runoptions:

0 commit comments

Comments
 (0)