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

我该如何解决这个问题?


当前回答

安装docker后,创建“docker”组并将用户添加到其中,编辑docker服务单元文件:

sudo nano /usr/lib/systemd/system/docker.service

在部分[Service]中添加两行:

SupplementaryGroups=docker    
ExecStartPost=/bin/chmod 666 /var/run/docker.sock

保存文件(Ctrl-X, y, Enter)

运行并启用Docker服务:

sudo systemctl daemon-reload
sudo systemctl start docker
sudo systemctl enable docker

其他回答

Sudo chmod 666 /var/run/docker.sock

这帮助我,当我得到错误,甚至登录到docker 但现在这在我的系统中工作得很好。

你可以尝试在https://docs.docker.com/install/linux/linux-postinstall/文档中以非根用户的身份管理Docker。

在这样做之后,如果问题仍然存在,那么您可以运行以下命令来解决它:

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

作为linux用户的最短答案->

简单地尝试任何命令作为超级用户与“sudo”

- sudo docker-compose up

我用命令解决了这个错误:

$ sudo chmod 666 /var/run/docker.sock