/var/run/docker.sock:没有这样的文件或目录

时间:2018-10-01 13:18:35

标签: docker ubuntu-16.04

1)我正在使用以下cmd运行docker容器:

$ docker run --name=xyz -d -e CONTAINER_NAME=xyz -e SSH_PORT=22 -e NWMODE=HOST -e XDG_RUNTIME_DIR=/run/user/0 --net=host -v /mnt:/mnt -v /dev:/dev -v /etc/sysconfig/network-scripts:/etc/sysconfig/network-scripts -v /:/hostroot/ -v /etc/hostname:/etc/host_hostname -v /etc/localtime:/etc/localtime -v /var/run/docker.sock:/var/run/docker.sock --privileged=true cf3681e04bfb

2)在容器内部,有一个a.py,在其中创建一个docker客户端,如下所示:

$ cat a.py
#!/usr/bin/python
from docker import Client as DC
dc = DC('unix://var/run/docker.sock', version='auto')

3)运行a.py会出现以下错误:

$ ./a.py /usr/local/lib/python2.7/dist-packages/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown. warnings.warn(warning, RequestsDependencyWarning) Traceback (most recent call last): File "./a.py", line 5, in <module> dc = DC('unix://var/run/docker.sock', version='auto') File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 99, in __init__ self._version = self._retrieve_server_version() File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 124, in _retrieve_server_version 'Error while fetching server API version: {0}'.format(e) docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', error(2, 'No such file or directory'))

4)我有一个docker守护进程在监听模式下与unix域套接字一起运行:

$ netstat --listen | grep docker unix 2 [ ACC ] STREAM LISTENING 11146 /var/run/docker.sock unix 2 [ ACC ] STREAM LISTENING 17471 /var/run/docker/libcontainerd/docker-containerd.sock unix 2 [ ACC ] STREAM LISTENING 17648 /run/docker/libnetwork/02833519b1ad75ad062f6443ab5c10e5d9c783c22beb98d8b8b41b2ee082cfcd.sock

5)在容器内,我得到:

$ curl --verbose --unix-socket /var/run/docker.sock http:/containers/json * Trying /var/run/docker.sock... * Immediate connect fail for /var/run/docker.sock: No such file or directory * Closing connection 0 curl: (7) Couldn't connect to server

6)如果我从容器中退出并在主机上运行相同的curl cmd,则工作正常:

$ exit //coming out from the container $ curl --unix-socket /var/run/docker.sock http:/containers/json [{"Id":"3973df106fcfae0d1f5b4be23b6e5f265421aa932053433f5e9e7bc9574d753a","Names"......multiple key-value pairs here...]

任何人都可以让我知道上述步骤3/5中可能引起错误的原因。还有什么可以尝试进一步调试它的。谢谢。

1 个答案:

答案 0 :(得分:0)

它将仅在节点上起作用。如果希望它在容器内运行,请确保使用卷hostPath方法挂载/ var / run。

curl --unix-socket /var/run/docker.sock http://docker/containers/json