gpt4 book ai didi

python - 连接到托管在远程服务器上的 docker

转载 作者:行者123 更新时间:2023-11-28 22:38:18 26 4
gpt4 key购买 nike

如何使用 python 连接到远程 docker 主机?

>>> from docker import Client
>>> cli = Client(base_url='tcp://52.90.216.176:2375')
>>>
>>> cli.containers()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/docker/api/container.py", line 69, in containers
res = self._result(self._get(u, params=params), True)
File "/usr/local/lib/python2.7/site-packages/docker/utils/decorators.py", line 47, in inner
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/docker/client.py", line 112, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 480, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 437, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='52.90.216.176', port=2375): Max retries exceeded with url: /v1.21/containers/json?all=0&limit=-1&trunc_cmd=0&size=0 (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fd87d836750>: Failed to establish a new connection: [Errno 111] Connection refused',))

如果我登录到 52.90.216.176 并使用以下命令:

>>> cli = Client(base_url='unix://var/run/docker.sock')

这行得通。但是如何连接到在另一台服务器上运行的 docker?

最佳答案

听起来您正在使用 docker-py。

此外,听起来您可能不熟悉 TLS,因此请阅读 using TLS with docker-py 的文档.您可能需要下载 TLS 文件并将它们复制到本地 docker-py 客户端,因为它们用于验证您是否有权连接到 Docker 守护进程。

我希望您的远程 Docker 守护进程没有暴露给世界。

如果它没有运行 TLS(向世界公开):

client = docker.Client(base_url='<https_url>', tls=False)

如果使用 TLS 保护(不向外界公开):

client = docker.Client(base_url='<https_url>', tls=True)

关于python - 连接到托管在远程服务器上的 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com