简体   繁体   English

/var/run/docker.sock在centos 7上运行的容器中无法访问

[英]/var/run/docker.sock unaccessible in container running on centos 7

I'm launching a container which runs a bash script that does a docker build internally using docker 1.3.2 on Centos 7.0.1406 . 我正在启动一个运行bash脚本的容器,该脚本使用Centos 7.0.1406上的docker 1.3.2在内部进行docker构建。 The files/commands are at https://gist.github.com/wrabbit-revisited/1d70d0f1805be1848c08 . 文件/命令位于https://gist.github.com/wrabbit-revisited/1d70d0f1805be1848c08

The docker build needs access to the docker socket so i use a common trick, as per http://nathanleclaire.com/blog/2014/07/12/10-docker-tips-and-tricks-that-will-make-you-sing-a-whale-song-of-joy/ : docker build需要访问docker socket,所以我使用了一个常见的技巧,如http://nathanleclaire.com/blog/2014/07/12/10-docker-tips-and-tricks-that-will-make-你唱一首鲸鱼之歌/

-v /var/run/docker.sock:/var/run/docker.sock

Prior to the build i run a check in the script: 在构建之前,我在脚本中运行检查:

if [ -e "/var/run/docker.sock" ];
then
  echo "docker.sock found"
else
  echo "docker.sock not found"
fi

and the "echo" shows that docker.sock is not found. 并且“echo”显示找不到docker.sock。 It is found if the check is done outside the container using sudo. 如果使用sudo在容器外部进行检查,则会发现。

I tried adding "--permissive=true" to the "docker run" command line, but no apparent change. 我尝试在“docker run”命令行中添加“--permissive = true”,但没有明显的变化。

There is some reference to a similar problem here: https://github.com/dpw/selinux-dockersock . 这里有一些类似问题的参考: https//github.com/dpw/selinux-dockersock It targets Fedora/RHEL, but doesn't resolve this issue, either. 它针对Fedora / RHEL,但也没有解决这个问题。 If i use "setenforce Permissive" and sestatus to ensure selinux is in permissive mode the issue remains unresolved. 如果我使用“setenforce Permissive”和sestatus来确保selinux处于许可模式,问题仍未得到解决。

I've also tried adding "--security-opt=label:type:docker_t" to the docker command line, as per https://github.com/jwilder/nginx-proxy/issues/40 . 我还尝试将“--security-opt = label:type:docker_t”添加到docker命令行,按照https://github.com/jwilder/nginx-proxy/issues/40 No apparent effect. 没有明显的效果。

The selinux policy for Docker is described here: http://www.unix.com/man-page/centos/8/docker_selinux/ . 这里描述了Docker的selinux策略: http//www.unix.com/man-page/centos/8/docker_selinux/

Lots of information, but i'm not sure if selinux is contributing to the problem. 很多信息,但我不确定selinux是否有助于解决问题。 If i edit /etc/selinux/config to disable selinux then reboot and run sestatus it says selinux is disabled, but the issue remains. 如果我编辑/ etc / selinux / config以禁用selinux然后重新启动并运行sestatus它说selinux被禁用,但问题仍然存在。

Looking about, it may be related to this: https://github.com/docker/compose/issues/983 . 看,它可能与此有关: https//github.com/docker/compose/issues/983 Using this trick to run docker inside a container is quite common but perhaps there is a better way to do this or a good workaround. 使用这个技巧在容器内运行docker是很常见的,但也许有更好的方法来做到这一点或一个好的解决方法。 I considered dind, but that's work and this is a widely-used, simple (on the surface), approach to running a docker build inside a container. 我认为是dind,但这是工作,这是一个广泛使用的,简单的(表面上),在容器内运行docker构建的方法。 There is probably a simple solution. 可能有一个简单的解决方案。

Any help would be appreciated! 任何帮助,将不胜感激! thanks 谢谢

I think your problem might be due to a misunderstanding of the -v option to docker run . 我认为您的问题可能是由于对docker run-v选项的误解。 You say you did 你说你做到了

-v /var/run/docker:/var/run/docker

This creates a bind mount in the container for the file or directory /var/run/docker . 这将在容器中为文件或目录/var/run/docker创建绑定装入。 But in your case, there is no such file or directory. 但在你的情况下,没有这样的文件或目录。 You want the file /var/run/docker.sock . 你想要文件/var/run/docker.sock So you need to do 所以你需要这样做

-v /var/run/docker.sock:/var/run/docker.sock

to bind mount that file into the container. 将该文件挂载到容器中。

As /var/run/docker didn't exist, you might wonder why docker didn't tell you about the error. 由于/var/run/docker不存在,您可能想知道为什么docker没有告诉您有关错误的信息。 But the -v option has the surprising behaviour that if the path does not exist on the host, docker will create it as a directory. 但是-v选项具有令人惊讶的行为,如果主机上不存在该路径,则docker会将其创建为目录。 So you end up with a useless empty /var/run/docker directory on the host and container. 所以你最终在主机和容器上有一个无用的空/var/run/docker目录。

In principle, you could also do -v /var/run:/var/run to bind mount the containing directory. 原则上,您也可以执行-v /var/run:/var/run来绑定挂载包含目录。 But it's probably a bad idea to give a container access to the host's /var/run directory tree. 但是,给容器访问主机的/var/run目录树可能是一个坏主意。

And as you are on CentOS, you will also need to use https://github.com/dpw/selinux-dockersock for access to /var/run/docker.sock to work with SELinux in enforcing mode. 当您使用CentOS时,您还需要使用https://github.com/dpw/selinux-dockersock访问/var/run/docker.sock,以便在执行模式下使用SELinux。

Confirmed installing https://github.com/dpw/selinux-dockersock is enough and good in the long run. 从长远来看,确认安装https://github.com/dpw/selinux-dockersock就足够了。

A quick alternative fix is to pass --privileged argument, when starting a container. 一个快速的替代方法是在启动容器时传递--privileged参数。

暂无
暂无

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

相关问题 在 /var/run 中链接 docker.sock - Link docker.sock in /var/run 向/var/run/docker.sock添加666权限的安全漏洞 - Security loopholes in adding 666 permissions to /var/run/docker.sock Pycharm Docker Unix / TCP套接字(使用unix:///var/run/docker.sock):权限被拒绝 - Pycharm Docker Unix / TCP socket (with unix:///var/run/docker.sock): Permission Denied 无法从 PHP 代码连接到 unix:///var/run/docker.sock(权限被拒绝) - Unable to connect to unix:///var/run/docker.sock (Permission denied) from PHP code 仅使用 python 套接字连接到 docker.sock 以获取容器统计信息 - Connect to docker.sock to get container stats using python socket only Docker无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)Ubuntu - Docker Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Ubuntu 不正确的默认套接字是/var/run/mysqld/mysqld.sock - ruby on rails incorrect default socket is /var/run/mysqld/mysqld.sock Ubuntu:无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2) - Ubuntu: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 无法通过套接字'/var/run/mysqld/mysqld.sock'(2)连接到本地MySQL服务器(错误2002) - Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (error 2002) NGINX: connect() to unix:/var/run/php7.2-fpm.sock failed (2: No such file or directory) - NGINX: connect() to unix:/var/run/php7.2-fpm.sock failed (2: No such file or directory)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM