You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已加入docker组,普通用户执行docker run hello-world失败,sudo执行正常

普通用户无需sudo运行Docker的问题解决

问题现象

普通用户执行docker run hello-world时出现错误:

$ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///home/unclefonso/.docker/desktop/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

但使用sudo执行则正常输出:

$ sudo docker run hello-world

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/

已尝试操作

已执行命令将用户加入docker组,但问题依旧:

$ sudo usermod -aG docker $USER
[sudo] password for unclefonso: 
mage@pop-os ~
$ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///home/unclefonso/.docker/desktop/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
mage@pop-os ~
$ groups unclefonso
unclefonso : unclefonso adm sudo kvm lpadmin docker

解决方法

1. 重新登录或重启会话

执行usermod加入组后,当前终端会话不会自动加载新的组权限。退出当前用户再重新登录,或者重启系统,让组变更生效。

2. 检查并重置DOCKER_HOST环境变量

错误提示连接的是Docker Desktop的socket文件,可能是环境变量DOCKER_HOST被设置为该路径。执行以下命令处理:

# 查看当前DOCKER_HOST设置
echo $DOCKER_HOST
# 如果输出是unix:///home/unclefonso/.docker/desktop/docker.sock,取消设置
unset DOCKER_HOST
# 永久生效需编辑~/.bashrc或~/.zshrc,删除设置DOCKER_HOST的行后重新加载配置
source ~/.bashrc

3. 验证Docker socket权限

检查系统默认的Docker socket权限,确保它属于docker组:

ls -l /var/run/docker.sock

如果输出的组不是docker,执行以下命令修改:

sudo chown root:docker /var/run/docker.sock

4. Docker Desktop权限配置(若使用Desktop版本)

如果安装的是Docker Desktop,打开设置面板:

  • 进入Resources > File Sharing,确保当前用户的主目录被添加到共享列表;
  • 或在Settings > Advanced中,开启允许普通用户访问Docker daemon的选项。

内容的提问来源于stack exchange,提问作者thefonso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:15:24