如何使用 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/