Skip to content

Resolved issue #3311 and #3304 #3313

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
46 changes: 25 additions & 21 deletions docker/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,26 +1074,27 @@ 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.

Args:
container (str or dict): The container to restart. If a dict, the
``Id`` key is used.
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.
container (str or dict): The container to restart. If a dict, the ``Id`` key is used.
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, optional): The signal to send when stopping the container. Defaults to None, which uses the server's default behavior.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
params = {'t': timeout}
if signal is not None:
params['signal'] = signal

url = self._url("/containers/{0}/restart", container)
conn_timeout = self.timeout
if conn_timeout is not None:
conn_timeout += timeout
conn_timeout += timeout
res = self._post(url, params=params, timeout=conn_timeout)
self._raise_for_status(res)

Expand Down Expand Up @@ -1184,30 +1185,33 @@ 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.

Args:
container (str): The container to stop
timeout (int): Timeout in seconds to wait for the container to
stop before sending a ``SIGKILL``. If None, then the
StopTimeout value of the container will be used.
Default: None
container (str): The container to stop.
timeout (int): Timeout in seconds to wait for the container to stop before sending a ``SIGKILL``. If None, then the StopTimeout value of the container will be used. Default: None.
signal (str, optional): The signal to send when stopping the container. If not specified, the server's default behavior is used.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
if timeout is None:
params = {}
timeout = 10
params = {}
timeout_val = 10
else:
params = {'t': timeout}
params = {'t': timeout}
timeout_val = timeout

if signal is not None:
params['signal'] = signal

url = self._url("/containers/{0}/stop", container)
conn_timeout = self.timeout
if conn_timeout is not None:
conn_timeout += timeout
conn_timeout += timeout_val
res = self._post(url, params=params, timeout=conn_timeout)
self._raise_for_status(res)

Expand Down
3 changes: 2 additions & 1 deletion docker/transport/unixconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def __init__(self, base_url, socket_path, timeout=60, maxsize=10):
self.timeout = timeout

def _new_conn(self):
self.num_connections += 1
return UnixHTTPConnection(
self.base_url, self.socket_path, self.timeout
self.base_url, self.socket_path, self.timeout
)


Expand Down