我在装有Ubuntu操作系统的机器上安装了Docker。 当我跑步时:

sudo docker run hello-world

一切都很好,但是我想隐藏sudo命令以使该命令更短。 如果我写的命令没有sudo

docker run hello-world

显示如下:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.35/containers/create: dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'.

当我试图跑步时,同样的情况也会发生:

docker-compose up

我该如何解决这个问题?


当前回答

在我的例子中,是进程本身(CI服务器代理)试图运行一个docker命令,但无法运行它,但当我试图从同一个用户中运行相同的命令时,它工作了。

重新启动运行CI服务器代理的守护进程解决了这个问题。

之前命令在代理内部无法工作的原因是因为代理在我安装docker并授予docker组权限之前正在运行,代理进程使用缓存的旧权限并且正在失败。重新启动进程将丢弃缓存,使事情得以解决。

其他回答

升级后,我的权限被拒绝了。 执行安装步骤后的“mkb”步骤不会改变任何东西,因为我的用户已经在“docker”组中;我试了两次都没有成功。

经过一个小时的搜索,下面的解决方案终于起作用了:

sudo chmod 666 /var/run/docker.sock

解决方案来自奥尔尚斯克。

看起来升级已经重新创建了套接字,但没有给docker组足够的权限。

问题

这个硬chmod打开安全漏洞,每次重启后,这个错误会一次又一次地启动,每次都必须重新执行上面的命令。我要一个彻底的解决办法。为此你有两个问题:

1) Problem with SystemD : The socket will be create only with owner 'root' and group 'root'. You can check this first problem with this command : ls -l /lib/systemd/system/docker.socket If every this is good, you should see 'root/docker' not 'root/root'. 2 ) Problem with graphical Login : https://superuser.com/questions/1348196/why-my-linux-account-only-belongs-to-one-group You can check this second problem with this command : groups If everything is correct you should see the docker group in the list. If not try the command sudo su $USER -c groups if you see then the docker group it is because of the bug.

解决方案

如果你设法得到一个图形登录的工作,这应该做的工作:

sudo chgrp docker /lib/systemd/system/docker.socket
sudo chmod g+w /lib/systemd/system/docker.socket

但如果你不能控制这个bug,一个不错的解决方案可以是:

sudo chgrp $USER /lib/systemd/system/docker.socket
sudo chmod g+w /lib/systemd/system/docker.socket

这种工作是因为您处于图形化环境中,并且可能是计算机上唯一的用户。 在这两种情况下,您都需要重新启动(或sudo chmod 666 /var/run/docker.sock)

修复Docker问题:(权限被拒绝)

如果docker组不存在,请创建docker组:sudo groupadd docker 查看可用系统中的超级用户数量:grep -Po '^sudo.+:\K。* $的所属 在linux命令shell中导出用户:Export user = demuser 将用户添加到docker组:sudo usermod -aG docker $ user 执行以下命令/登录或注销 检查docker运行是否正常:docker run hello-world 如果仍然得到错误,重新启动:Reboot

如果不成功,执行以下命令:

Sudo chmod 660 /var/run/docker.sock

Docker守护进程绑定到Unix套接字,而不是TCP端口。 默认情况下,Unix套接字由root用户拥有,其他用户只能使用sudo访问它。Docker守护进程始终以根用户运行。

如果不想在docker命令前面加上sudo,可以创建一个名为docker的Unix组,并向其中添加用户。当Docker守护进程启动时,它会创建一个Unix套接字,供Docker组的成员访问。

创建docker组并添加用户:

Create the docker group sudo groupadd docker Add your user to the docker group sudo usermod -aG docker $USER Log out and log back in so that your group membership is re-evaluated. If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect. On a desktop Linux environment such as X Windows, log out of your session completely and then log back in. On Linux, you can also run the following command to activate the changes to groups: newgrp docker Verify that you can run docker commands without sudo. The below command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits docker run hello-world

如果您在将用户添加到Docker组之前首先使用sudo运行Docker CLI命令,您可能会看到以下错误,这表明您的~/。由于sudo命令,创建的Docker /目录权限不正确。

   WARNING: Error loading config file: /home/user/.docker/config.json -
   stat /home/user/.docker/config.json: permission denied

要解决这个问题,要么删除~/。Docker /目录(它会自动重新创建,但任何自定义设置都将丢失),或使用以下命令更改其所有权和权限:

sudo chown "$USER":"$USER" /home/"$USER"/.docker -R

sudo chmod g+rwx "$HOME/.docker" -R

linux上docker的所有其他后期安装步骤都可以在这里找到https://docs.docker.com/engine/install/linux-postinstall/

将当前用户添加到docker组

sudo usermod -aG docker $USER

修改docker套接字的权限,使其能够连接 到docker守护进程/var/run/docker.sock

sudo chmod 666 /var/run/docker.sock

在Centos上安装Docker后。在运行下面的命令时,我得到下面的错误。

[centos@aiops-dev-cassandra3 ~]$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.soc k/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

修改docker.socket的Group和Permission

[centos@aiops-dev-cassandra3 ~]$ ls -l /lib/systemd/system/docker.socket
-rw-r--r--. 1 root root 197 Nov 13 07:25 /lib/systemd/system/docker.socket
[centos@aiops-dev-cassandra3 ~]$ sudo chgrp docker /lib/systemd/system/docker.socket
[centos@aiops-dev-cassandra3 ~]$ sudo chmod 666 /var/run/docker.sock
[centos@aiops-dev-cassandra3 ~]$ ls -lrth /var/run/docker.sock
srw-rw-rw-. 1 root docker 0 Nov 20 11:59 /var/run/docker.sock
[centos@aiops-dev-cassandra3 ~]$

使用以下docker命令进行验证

[centos@aiops-dev-cassandra3 ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[centos@aiops-dev-cassandra3 ~]$