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

我该如何解决这个问题?


当前回答

在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 ~]$

其他回答

最直接的解决办法是打字

Sudo chmod 666 /var/run/docker.sock 每次你启动你的机器。然而,这种方法会破坏现有的任何系统安全性,并向所有人开放Docker套接字。如果这对你来说是可以接受的——例如:你的机器的唯一用户——那么就使用它。

尽管如此,每次启动机器时都需要它,您可以通过添加使它在引导时运行

start on startup
task
exec chmod 666 /var/run/docker.sock

打开/etc/init/docker-chmod.conf文件。

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

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

这对我来说很有用:

进入容器内部并修改文件的ACL

sudo usermod -aG docker $USER
sudo setfacl --modify user:$USER:rw /var/run/docker.sock

这是一个比使用chmod更好的解决方案。

为了解决这个问题,我搜索了我的docker和docker-compose安装在哪里。在我的例子中,docker安装在/usr/bin/docker, docker-compose安装在/usr/local/bin/docker-compose路径下。然后,我在我的终端中这样写:

码头工人:

sudo chmod +x /usr/bin/docker

docker-compose:

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

现在我不需要在我的命令docker中写入单词sudo

/***********************************************************************/

勘误:

这个问题的最佳解决方案由@mkasberg评论。我引用评论:

这可能会起作用,你可能会遇到问题。此外,这也是一个安全漏洞。你最好把自己加入docker组,就像文档中说的那样。sudo groupadd docker, sudo usermod -aG docker $USER。 文档:https://docs.docker.com/install/linux/linux-postinstall/

如果你想以非root用户运行docker,那么你需要将它添加到docker组中。

如果docker组不存在,请创建docker组

$ sudo groupadd docker

将您的用户添加到docker组。

$ sudo usermod -aG docker $USER

登录到新的docker组(以避免再次注销/登录;但如果还不够,请尝试重新启动):

$ newgrp docker

检查docker是否可以在没有root的情况下运行

$ docker run hello-world

如果仍然有错误,重新启动

$ reboot

警告

docker组授予等同于root用户的权限。有关这如何影响系统安全性的详细信息,请参阅Docker Daemon攻击面。

摘自docker官方文档: manage-docker-as-a-non-root-user