Skip to content

Commit e50725f

Browse files
committed
feat: add signal parameter to restart and stop methods in Container API
Signed-off-by: Khushiyant <khushiyant2002@gmail.com>
1 parent db7f8b8 commit e50725f

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docker/api/container.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def resize(self, container, height, width):
10741074
self._raise_for_status(res)
10751075

10761076
@utils.check_resource('container')
1077-
def restart(self, container, timeout=10):
1077+
def restart(self, container, timeout=10, signal=None):
10781078
"""
10791079
Restart a container. Similar to the ``docker restart`` command.
10801080
@@ -1084,6 +1084,7 @@ def restart(self, container, timeout=10):
10841084
timeout (int): Number of seconds to try to stop for before killing
10851085
the container. Once killed it will then be restarted. Default
10861086
is 10 seconds.
1087+
signal (str or int): The signal to send. Defaults to ``SIGTERM``
10871088
10881089
Raises:
10891090
:py:class:`docker.errors.APIError`
@@ -1092,6 +1093,10 @@ def restart(self, container, timeout=10):
10921093
params = {'t': timeout}
10931094
url = self._url("/containers/{0}/restart", container)
10941095
conn_timeout = self.timeout
1096+
if signal is not None:
1097+
if not isinstance(signal, str):
1098+
signal = int(signal)
1099+
params["signal"] = signal
10951100
if conn_timeout is not None:
10961101
conn_timeout += timeout
10971102
res = self._post(url, params=params, timeout=conn_timeout)
@@ -1184,7 +1189,7 @@ def stats(self, container, decode=None, stream=True, one_shot=None):
11841189
return self._result(self._get(url, params=params), json=True)
11851190

11861191
@utils.check_resource('container')
1187-
def stop(self, container, timeout=None):
1192+
def stop(self, container, timeout=None, signal=None):
11881193
"""
11891194
Stops a container. Similar to the ``docker stop`` command.
11901195
@@ -1194,6 +1199,7 @@ def stop(self, container, timeout=None):
11941199
stop before sending a ``SIGKILL``. If None, then the
11951200
StopTimeout value of the container will be used.
11961201
Default: None
1202+
signal (str or int): The signal to send. Defaults to ``SIGTERM``
11971203
11981204
Raises:
11991205
:py:class:`docker.errors.APIError`
@@ -1206,6 +1212,10 @@ def stop(self, container, timeout=None):
12061212
params = {'t': timeout}
12071213
url = self._url("/containers/{0}/stop", container)
12081214
conn_timeout = self.timeout
1215+
if signal is not None:
1216+
if not isinstance(signal, str):
1217+
signal = int(signal)
1218+
params["signal"] = signal
12091219
if conn_timeout is not None:
12101220
conn_timeout += timeout
12111221
res = self._post(url, params=params, timeout=conn_timeout)

docker/models/containers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def restart(self, **kwargs):
402402
timeout (int): Number of seconds to try to stop for before killing
403403
the container. Once killed it will then be restarted. Default
404404
is 10 seconds.
405-
405+
signal (str or int): The signal to send. Defaults to ``SIGTERM``
406406
Raises:
407407
:py:class:`docker.errors.APIError`
408408
If the server returns an error.
@@ -445,7 +445,7 @@ def stop(self, **kwargs):
445445
Args:
446446
timeout (int): Timeout in seconds to wait for the container to
447447
stop before sending a ``SIGKILL``. Default: 10
448-
448+
signal (str or int): The signal to send. Defaults to ``SIGTERM``
449449
Raises:
450450
:py:class:`docker.errors.APIError`
451451
If the server returns an error.

tests/unit/models_containers_test.py

+28
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,34 @@ def test_image(self):
726726
container = client.containers.get(FAKE_CONTAINER_ID)
727727
assert container.image.id == FAKE_IMAGE_ID
728728

729+
def test_restart_with_signal(self):
730+
client = make_fake_client()
731+
container = client.containers.get(FAKE_CONTAINER_ID)
732+
container.restart(timeout=2, signal='SIGTERM')
733+
client.api.restart.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
734+
signal='SIGTERM')
735+
736+
def test_stop_with_signal(self):
737+
client = make_fake_client()
738+
container = client.containers.get(FAKE_CONTAINER_ID)
739+
container.stop(timeout=2, signal='SIGTERM')
740+
client.api.stop.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
741+
signal='SIGTERM')
742+
743+
def test_restart_with_sigint(self):
744+
client = make_fake_client()
745+
container = client.containers.get(FAKE_CONTAINER_ID)
746+
container.restart(timeout=2, signal=2)
747+
client.api.restart.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
748+
signal=2)
749+
750+
def test_stop_with_sigint(self):
751+
client = make_fake_client()
752+
container = client.containers.get(FAKE_CONTAINER_ID)
753+
container.stop(timeout=2, signal=2)
754+
client.api.stop.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
755+
signal=2)
756+
729757
def test_kill(self):
730758
client = make_fake_client()
731759
container = client.containers.get(FAKE_CONTAINER_ID)

0 commit comments

Comments
 (0)