diff --git a/docker/api/container.py b/docker/api/container.py index d1b870f9c..738792f52 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -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. @@ -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` @@ -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) @@ -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. @@ -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` @@ -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) diff --git a/docker/models/containers.py b/docker/models/containers.py index 9c9e92c90..1eb6b21d0 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -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. @@ -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. diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index 0e2ae341a..59e0b2bcc 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -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)