Docker (compose): connection refused without sudo



一切都很好,直到"发生了什么事"。之后,我可以使用dockerdocker-composesudo,但没有它,docker将无限期挂起,docker-compose将返回连接堆栈跟踪。

行不通:

  • 在没有sudo的情况下运行docker会无限期挂起。暂时修复了导出DOCKER_HOST=tcp://127.0.0.1:2375.
  • docker-compose没有sudo返回connection refused。它不是关于连接到特定的容器,而是所有的基本命令,如docker-compose ps

证明用户属于docker组:

kretyn@junk$ groups kretyn
kretyn : kretyn adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

解决方案提示我激活了一个docker上下文,该上下文指向现有但未激活的主机。删除该上下文并取消设置DOCKER_HOST允许docker命令正确工作。这是一个惊喜,因为我在写这个问题之前重新安装了(清除了我能找到的所有内容)docker。

删除上下文是不够的。Docker Compose需要显式的docker context use default,否则无法理解。

docker-compose ps的问题仍然存在。

docker-compose的堆栈跟踪

kretyn@junk$ docker-compose ps
Traceback (most recent call last):
File "urllib3/connection.py", line 159, in _new_conn
File "urllib3/util/connection.py", line 84, in create_connection
File "urllib3/util/connection.py", line 74, in create_connection
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "urllib3/connectionpool.py", line 670, in urlopen
File "urllib3/connectionpool.py", line 392, in _make_request
File "http/client.py", line 1255, in request
File "http/client.py", line 1301, in _send_request
File "http/client.py", line 1250, in endheaders
File "http/client.py", line 1010, in _send_output
File "http/client.py", line 950, in send
File "urllib3/connection.py", line 187, in connect
File "urllib3/connection.py", line 171, in _new_conn
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f762c4e3e20>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "requests/adapters.py", line 439, in send
File "urllib3/connectionpool.py", line 726, in urlopen
File "urllib3/util/retry.py", line 446, in increment
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f762c4e3e20>: Failed to establish a new connection: [Errno 111] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "docker/api/client.py", line 214, in _retrieve_server_version
File "docker/api/daemon.py", line 181, in version
File "docker/utils/decorators.py", line 46, in inner
File "docker/api/client.py", line 237, in _get
File "requests/sessions.py", line 543, in get
File "requests/sessions.py", line 530, in request
File "requests/sessions.py", line 643, in send
File "requests/adapters.py", line 516, in send
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f762c4e3e20>: Failed to establish a new connection: [Errno 111] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose/cli/main.py", line 80, in main
File "compose/cli/main.py", line 189, in perform_command
File "compose/cli/command.py", line 60, in project_from_options
File "compose/cli/command.py", line 152, in get_project
File "compose/cli/docker_client.py", line 41, in get_client
File "compose/cli/docker_client.py", line 170, in docker_client
File "docker/api/client.py", line 197, in __init__
File "docker/api/client.py", line 221, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f762c4e3e20>: Failed to establish a new connection: [Errno 111] Connection refused'))
[46145] Failed to execute script docker-compose

版本/构建

Linux发行版

kretyn@junk$ uname -a
Linux junk 5.8.0-43-generic #49~20.04.1-Ubuntu SMP Fri Feb 5 09:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

码头工人

kretyn@junk$ sudo docker system info
Client:
Context:    default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 9
Running: 8
Paused: 0
Stopped: 1
Images: 44
Server Version: 20.10.3
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.8.0-43-generic
Operating System: Ubuntu 20.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 12
Total Memory: 15.29GiB
Name: meland
ID: 3PE6:4AWU:LGRR:HOBJ:XUCJ:ER4H:ZEF5:MWUJ:COEG:WPK5:YH2U:FKUF
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: No blkio weight support
WARNING: No blkio weight_device support

Docker-compose

kretyn@junk$ sudo docker-compose version
docker-compose version 1.28.0, build d02a7b1a
docker-py version: 4.4.1
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019

问题是context条目失效。

解决方案使用docker context rm remote删除名为remote的死亡上下文。

尽管docker context ls默认指向default,但为了正确更新上下文,必须执行docker context use default

作为部分修复,我之前将DOCKER_HOST设置为tcp://127.0.0.1:2375。更新上下文后,必须取消设置env变量,即unset DOCKER_HOST

惰性命令,假设上下文remote:

$ docker context rm remote
$ docker context use default
$ unset DOCKER_HOST

的背景有一个基于ssh端点的remote上下文。我的ssh配置设置的名称指向我的域名,但最近它背后的主机被删除了。

有趣的是,我一直在"修复"。其中一个步骤是从我的本地Ubuntu主机上完全清除docker,包括apt,/var/lib/docker,/etc/docker~/.local/中的随机脚本。然而,context没有被清洗。

david的解决方案非常适合我。

我想提到的是,即使你的sudo docker context ls只显示默认上下文,你的docker context ls可能会显示不同的(和潜在的破碎)上下文。你想删除它们

但是问题是你的docker ps挂起了。

所以在你尝试docker rm之前,你需要通过DOCKER_HOST=tcp://127.0.0.1:2375覆盖上下文。

因此,完整的解决方案是
DOCKER_HOST=tcp://127.0.0.1:2375
docker context ls
docker context rm <broken context name>
docker context use <valid context name(maybe default?)>
unset DOCKER_HOST

,然后确保没有sudo的所有docker命令都可以工作。

docker ps

如果您没有使用docker或docker-compose的必要权限,请查看文档

sudo usermod -aG docker

sudo chmod +x/usr/local/bin/docker-compose

你必须重新启动你的终端。

最新更新