Skip to content

Signal Parameter to Container Stop and Restart #3322

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions docker/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ def resize(self, container, height, width):
self._raise_for_status(res)

@utils.check_resource('container')
def restart(self, container, timeout=10):
def restart(self, container, timeout=10, signal=None):
"""
Restart a container. Similar to the ``docker restart`` command.

Expand All @@ -1084,6 +1084,7 @@ def restart(self, container, timeout=10):
timeout (int): Number of seconds to try to stop for before killing
the container. Once killed it will then be restarted. Default
is 10 seconds.
signal (str or int): The signal to send. Defaults to ``SIGTERM``

Raises:
:py:class:`docker.errors.APIError`
Expand All @@ -1092,6 +1093,10 @@ def restart(self, container, timeout=10):
params = {'t': timeout}
url = self._url("/containers/{0}/restart", container)
conn_timeout = self.timeout
if signal is not None:
if not isinstance(signal, str):
signal = int(signal)
params["signal"] = signal
if conn_timeout is not None:
conn_timeout += timeout
res = self._post(url, params=params, timeout=conn_timeout)
Expand Down Expand Up @@ -1184,7 +1189,7 @@ def stats(self, container, decode=None, stream=True, one_shot=None):
return self._result(self._get(url, params=params), json=True)

@utils.check_resource('container')
def stop(self, container, timeout=None):
def stop(self, container, timeout=None, signal=None):
"""
Stops a container. Similar to the ``docker stop`` command.

Expand All @@ -1194,6 +1199,7 @@ def stop(self, container, timeout=None):
stop before sending a ``SIGKILL``. If None, then the
StopTimeout value of the container will be used.
Default: None
signal (str or int): The signal to send. Defaults to ``SIGTERM``

Raises:
:py:class:`docker.errors.APIError`
Expand All @@ -1206,6 +1212,10 @@ def stop(self, container, timeout=None):
params = {'t': timeout}
url = self._url("/containers/{0}/stop", container)
conn_timeout = self.timeout
if signal is not None:
if not isinstance(signal, str):
signal = int(signal)
params["signal"] = signal
if conn_timeout is not None:
conn_timeout += timeout
res = self._post(url, params=params, timeout=conn_timeout)
Expand Down
4 changes: 2 additions & 2 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def restart(self, **kwargs):
timeout (int): Number of seconds to try to stop for before killing
the container. Once killed it will then be restarted. Default
is 10 seconds.

signal (str or int): The signal to send. Defaults to ``SIGTERM``
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
Expand Down Expand Up @@ -445,7 +445,7 @@ def stop(self, **kwargs):
Args:
timeout (int): Timeout in seconds to wait for the container to
stop before sending a ``SIGKILL``. Default: 10

signal (str or int): The signal to send. Defaults to ``SIGTERM``
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,34 @@ def test_image(self):
container = client.containers.get(FAKE_CONTAINER_ID)
assert container.image.id == FAKE_IMAGE_ID

def test_restart_with_signal(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
container.restart(timeout=2, signal='SIGTERM')
client.api.restart.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
signal='SIGTERM')

def test_stop_with_signal(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
container.stop(timeout=2, signal='SIGTERM')
client.api.stop.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
signal='SIGTERM')

def test_restart_with_sigint(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
container.restart(timeout=2, signal=2)
client.api.restart.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
signal=2)

def test_stop_with_sigint(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
container.stop(timeout=2, signal=2)
client.api.stop.assert_called_with(FAKE_CONTAINER_ID, timeout=2,
signal=2)

def test_kill(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
Expand Down