如何修复 docker:获得权限被拒绝问题 [英] How to fix docker: Got permission denied issue
问题描述
我在装有 Ubuntu 操作系统的机器上安装了 Docker.
当我跑步时:
I installed Docker in my machine where I have Ubuntu OS.
When I run:
sudo docker run hello-world
一切正常,但我想隐藏 sudo
命令以使命令更短.
如果我在没有 sudo
All is ok, but I want to hide the sudo
command to make the command shorter.
If I write the command without 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'.
当我尝试运行时也会发生同样的情况:
The same happens when I try to run:
docker-compose up
我该如何解决这个问题?
How can I resolve this?
推荐答案
如果你想以非 root 用户身份运行 docker,则需要将其添加到 docker 组.
If you want to run docker as non-root user then you need to add it to the docker group.
- 如果 docker 组不存在,则创建它
$ sudo groupadd docker
- 将您的用户添加到 docker 组.
$ sudo usermod -aG docker $USER
- 运行以下命令或注销并再次登录并运行(这不起作用,您可能需要先重新启动机器)
$ newgrp docker
- 检查docker是否可以在没有root的情况下运行
$ docker run hello-world
如果仍然出现错误则重新启动
Reboot if still got error
$ reboot
警告
docker 组授予相当于 root 用户的权限.有关这如何影响系统安全性的详细信息,请参阅 Docker Daemon Attack Surface..
The docker group grants privileges equivalent to the root user. For details on how this impacts security in your system, see Docker Daemon Attack Surface..
摘自docker官方文档:manage-docker-as-非root用户
Taken from the docker official documentation: manage-docker-as-a-non-root-user
这篇关于如何修复 docker:获得权限被拒绝问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!