我在装有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

我该如何解决这个问题?


当前回答

升级后,我的权限被拒绝了。 执行安装步骤后的“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)

其他回答

你可以按照以下步骤来做,这会对你有用:

create a docker group sudo groupadd docker add your user to this group sudo usermod -aG docker $USER list the groups to make sure that docker group created successfully by running this command groups run the following command also to change the session for docker group newgrp docker change the group ownership for file docker.socksudo chown root:docker /var/run/docker.sock change the ownership for .docker directory sudo chown "$USER":"$USER" /home/"$USER"/.docker -R finally sudo chmod g+rwx "$HOME/.docker" -R

在这个测试之后,你可以运行docker ps -a

Ubuntu 21.04 systemd套接字所有权

让我先说明一下,这是一个非常适合我在本地开发期间的解决方案,我在这里搜索ubuntu docker权限错误,所以我就把这个留在这里。

unix套接字不是我的,所以我学会了。

sudo chown $(whoami):$(whoami) /var/run/docker.sock

对于您的开发环境,另一个更持久的解决方案是修改unix套接字创建的用户所有权。这将给你的用户所有权,所以它将在重启之间坚持:

sudo nano /etc/systemd/system/sockets.target.wants/docker.socket

docker.socket:

[Unit]
Description=Docker Socket for the API

[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=YOUR_USERNAME_HERE
SocketGroup=docker

[Install]
WantedBy=sockets.target

在unix:///var/run/ Docker上尝试连接Docker守护进程套接字时被拒绝权限。sock: http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/json:拨号unix /var/run/docker。Sock: connect:拒绝权限

sudo chmod 666 /var/run/docker.sock

这解决了我的问题。

这肯定不是问题所在,但由于它是谷歌错误消息时的第一个搜索结果,所以我就把它留在这里。

首先,使用以下命令检查docker服务是否正在运行:

Systemctl status docker.service

如果它没有运行,试着启动它:

Sudo systemctl start docker.service

... 并再次检查状态:

Systemctl status docker.service

如果还没有启动,请调查原因。可能,您修改了配置文件并犯了一个错误(就像我在修改/etc/docker/daemon.json时所做的那样)。

升级后,我的权限被拒绝了。 执行安装步骤后的“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)