This commit is contained in:
Bhaskar srinivas Kurukuri 2025-06-01 18:48:38 -03:00 committed by GitHub
commit cb353c74e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 22 deletions

View File

@ -1074,26 +1074,27 @@ class ContainerApiMixin:
self._raise_for_status(res) self._raise_for_status(res)
@utils.check_resource('container') @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. Restart a container. Similar to the ``docker restart`` command.
Args: Args:
container (str or dict): The container to restart. If a dict, the container (str or dict): The container to restart. If a dict, the ``Id`` key is used.
``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.
timeout (int): Number of seconds to try to stop for before killing signal (str, optional): The signal to send when stopping the container. Defaults to None, which uses the server's default behavior.
the container. Once killed it will then be restarted. Default
is 10 seconds.
Raises: Raises:
:py:class:`docker.errors.APIError` :py:class:`docker.errors.APIError`
If the server returns an error. If the server returns an error.
""" """
params = {'t': timeout} params = {'t': timeout}
if signal is not None:
params['signal'] = signal
url = self._url("/containers/{0}/restart", container) url = self._url("/containers/{0}/restart", container)
conn_timeout = self.timeout conn_timeout = self.timeout
if conn_timeout is not None: if conn_timeout is not None:
conn_timeout += timeout conn_timeout += timeout
res = self._post(url, params=params, timeout=conn_timeout) res = self._post(url, params=params, timeout=conn_timeout)
self._raise_for_status(res) self._raise_for_status(res)
@ -1184,30 +1185,33 @@ class ContainerApiMixin:
return self._result(self._get(url, params=params), json=True) return self._result(self._get(url, params=params), json=True)
@utils.check_resource('container') @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. Stops a container. Similar to the ``docker stop`` command.
Args: Args:
container (str): The container to stop container (str): The container to stop.
timeout (int): Timeout in seconds to wait for the container to 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.
stop before sending a ``SIGKILL``. If None, then the signal (str, optional): The signal to send when stopping the container. If not specified, the server's default behavior is used.
StopTimeout value of the container will be used.
Default: None
Raises: Raises:
:py:class:`docker.errors.APIError` :py:class:`docker.errors.APIError`
If the server returns an error. If the server returns an error.
""" """
if timeout is None: if timeout is None:
params = {} params = {}
timeout = 10 timeout_val = 10
else: 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) url = self._url("/containers/{0}/stop", container)
conn_timeout = self.timeout conn_timeout = self.timeout
if conn_timeout is not None: if conn_timeout is not None:
conn_timeout += timeout conn_timeout += timeout_val
res = self._post(url, params=params, timeout=conn_timeout) res = self._post(url, params=params, timeout=conn_timeout)
self._raise_for_status(res) self._raise_for_status(res)

View File

@ -37,8 +37,9 @@ class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
self.timeout = timeout self.timeout = timeout
def _new_conn(self): def _new_conn(self):
self.num_connections += 1
return UnixHTTPConnection( return UnixHTTPConnection(
self.base_url, self.socket_path, self.timeout self.base_url, self.socket_path, self.timeout
) )