尝试在执行docker stop时连接到Docker守护程序套接字时获得权限被拒绝

时间:2017-10-15 19:44:13

标签: sockets ubuntu docker

我的docker上运行了3个容器,我需要使用以下命令停止所有容器:

sudo docker stop $(docker ps -q)

当运行命令时,我收到了这条消息:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.32/containers/json: dial unix /var/run/docker.sock: connect: permission denied
See 'docker stop --help'.
Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers

我进行了一些搜索,并且显示消息的案例不适用于我的环境。我使用Ubuntu 16.04 LTS和Docker版本17.09.0-ce,构建afdb6d4

这条消息是什么意思?

3 个答案:

答案 0 :(得分:8)

If you are getting "permission denied" that probably means you haven't added yourself in users group which can operate upon docker. To fix that, go to your terminal and type:

sudo usermod -aG docker <name-of-user-to-grant-permission>

The 'docker' parameter is group created upon installing docker, and you can check that by typing:

getent group | grep docker

And the second parameter is the user you are adding to the group. The list of users you can check by typing:

getent passwd

For more information about command usermod you can find here.

UPDATED: I installed docker again and just remembered that when you apply this command you need to restart your machine.

答案 1 :(得分:7)

sudo usermod -a -G docker $USER

重新启动,然后运行:

docker container run hello-world

它在ubuntu 18.2上为我工作

答案 2 :(得分:4)

您的用户似乎无法使用docker命令,因此您还需要在括号中通过sudo运行它:

sudo docker stop $(sudo docker ps -q)