简体   繁体   English

Python docker-py 连接被拒绝

[英]Python docker-py Connection Refused

I am having trouble accessing docker daemon from a client using docker-py in Python.我在 Python 中使用docker-py从客户端访问 docker 守护进程时遇到问题。 I started a docker daemon by the command sudo docker -d & and the output was [1] 4894 .我通过命令sudo docker -d &启动了一个 docker 守护进程,输出为[1] 4894 Then I tried to access the daemon from python using the code that I got from here as root然后我尝试使用我从这里作为 root 获得的代码从 python 访问守护进程

from docker import Client
cli = Client(base_url='unix://var/run/docker.sock')
cli.containers()

This gave me the error:这给了我错误:

requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))

I also tried我也试过

cli = Client(base_url='tcp://127.0.0.1:4894') 

but it gave me the same error.但它给了我同样的错误。

This seems that the /var/run/docker.sock file has the incorrect permissions.这似乎是 /var/run/docker.sock 文件的权限不正确。 As the docker daemon is started as root the permissions are probably to restrictive.由于 docker 守护进程以 root 身份启动,因此权限可能会受到限制。

If you change the permissions to allow other users to access it you should have more success (eg o=rwx).如果您更改权限以允许其他用户访问它,您应该会获得更多成功(例如 o=rwx)。

The issue is indeed that /var/run/docker.sock has the incorrect permissions.问题确实是/var/run/docker.sock具有不正确的权限。 To fix it, you need to give the current user access to this file.要修复它,您需要授予当前用户访问此文件的权限。

However, on Linux, giving o=rwx rights to /var/run/docker.sock is very dangerous as it allows any user and service on the system to run commands as root.然而,在 Linux 上,赋予/var/run/docker.sock o=rwx权限是非常危险的,因为它允许系统上的任何用户和服务以 root 身份运行命令。 Indeed access to /var/run/docker.sock implies full root access to the machine.实际上访问/var/run/docker.sock意味着对机器的完全根访问权限。 See https://docs.docker.com/engine/security/#docker-daemon-attack-surface请参阅https://docs.docker.com/engine/security/#docker-daemon-attack-surface

A less dangerous approach consists in creating the group docker and adding the current user to this group.一种不太危险的方法是创建组docker并将当前用户添加到该组。 See https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user请参阅https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user

However, this approach is still potentially dangerous as it gives the current user full root access without the protections that sudo offers (ie, asking the user password from time to time and logging sudo calls.但是,这种方法仍然存在潜在危险,因为它在没有 sudo 提供的保护的情况下为当前用户提供了完全的 root 访问权限(即,不时询问用户密码并记录 sudo 调用。

See also What is the Docker security risk of /var/run/docker.sock?另请参阅/var/run/docker.sock 的 Docker 安全风险是什么?

(I unfortunately cannot comment hence I write my comment as an answer.) (不幸的是,我无法发表评论,因此我写下我的评论作为答案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM