Skip to content

Commit ce8a6ea

Browse files
authored
Merge pull request #1620 from docker/healthcheck-start-period
Add support for start_period in Healthcheck spec
2 parents 75e850e + 1ea6618 commit ce8a6ea

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

docker/api/container.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ def create_container(self, image, command=None, hostname=None, user=None,
418418
networking_config (dict): A networking configuration generated
419419
by :py:meth:`create_networking_config`.
420420
runtime (str): Runtime to use with this container.
421+
healthcheck (dict): Specify a test to perform to check that the
422+
container is healthy.
421423
422424
Returns:
423425
A dictionary with an image 'Id' key and a 'Warnings' key.

docker/models/containers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ def run(self, image, command=None, stdout=True, stderr=False,
516516
container, as a mapping of hostname to IP address.
517517
group_add (:py:class:`list`): List of additional group names and/or
518518
IDs that the container process will run as.
519+
healthcheck (dict): Specify a test to perform to check that the
520+
container is healthy.
519521
hostname (str): Optional hostname for the container.
520522
init (bool): Run an init inside the container that forwards
521523
signals and reaps processes

docker/types/containers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,17 @@ def __init__(
565565
'stop_timeout was only introduced in API version 1.25'
566566
)
567567

568-
if healthcheck is not None and version_lt(version, '1.24'):
569-
raise errors.InvalidVersion(
570-
'Health options were only introduced in API version 1.24'
571-
)
568+
if healthcheck is not None:
569+
if version_lt(version, '1.24'):
570+
raise errors.InvalidVersion(
571+
'Health options were only introduced in API version 1.24'
572+
)
573+
574+
if version_lt(version, '1.29') and 'StartPeriod' in healthcheck:
575+
raise errors.InvalidVersion(
576+
'healthcheck start period was introduced in API '
577+
'version 1.29'
578+
)
572579

573580
if isinstance(command, six.string_types):
574581
command = split_command(command)

docker/types/healthcheck.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ def __init__(self, **kwargs):
1212
interval = kwargs.get('interval', kwargs.get('Interval'))
1313
timeout = kwargs.get('timeout', kwargs.get('Timeout'))
1414
retries = kwargs.get('retries', kwargs.get('Retries'))
15+
start_period = kwargs.get('start_period', kwargs.get('StartPeriod'))
1516

1617
super(Healthcheck, self).__init__({
1718
'Test': test,
1819
'Interval': interval,
1920
'Timeout': timeout,
20-
'Retries': retries
21+
'Retries': retries,
22+
'StartPeriod': start_period
2123
})
2224

2325
@property
@@ -51,3 +53,11 @@ def retries(self):
5153
@retries.setter
5254
def retries(self, value):
5355
self['Retries'] = value
56+
57+
@property
58+
def start_period(self):
59+
return self['StartPeriod']
60+
61+
@start_period.setter
62+
def start_period(self, value):
63+
self['StartPeriod'] = value

tests/integration/api_healthcheck_test.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def test_healthcheck_passes(self):
2828
container = self.client.create_container(
2929
BUSYBOX, 'top', healthcheck=dict(
3030
test="true",
31-
interval=1*SECOND,
32-
timeout=1*SECOND,
31+
interval=1 * SECOND,
32+
timeout=1 * SECOND,
3333
retries=1,
3434
))
3535
self.tmp_containers.append(container)
@@ -41,10 +41,27 @@ def test_healthcheck_fails(self):
4141
container = self.client.create_container(
4242
BUSYBOX, 'top', healthcheck=dict(
4343
test="false",
44-
interval=1*SECOND,
45-
timeout=1*SECOND,
44+
interval=1 * SECOND,
45+
timeout=1 * SECOND,
4646
retries=1,
4747
))
4848
self.tmp_containers.append(container)
4949
self.client.start(container)
5050
wait_on_health_status(self.client, container, "unhealthy")
51+
52+
@helpers.requires_api_version('1.29')
53+
def test_healthcheck_start_period(self):
54+
container = self.client.create_container(
55+
BUSYBOX, 'top', healthcheck=dict(
56+
test="echo 'x' >> /counter.txt && "
57+
"test `cat /counter.txt | wc -l` -ge 3",
58+
interval=1 * SECOND,
59+
timeout=1 * SECOND,
60+
retries=1,
61+
start_period=3 * SECOND
62+
)
63+
)
64+
65+
self.tmp_containers.append(container)
66+
self.client.start(container)
67+
wait_on_health_status(self.client, container, "healthy")

0 commit comments

Comments
 (0)