环境信息
- 操作系统:Ubuntu 20.04 LTS 桌面版
- Docker:19.03.10
现状
- 当前账号是willzhao,直接执行docker xxx命令会报以下错误:
willzhao@ideapad:~$ docker images
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.40/images/json: dial unix /var/run/docker.sock: connect: permission denied
- 执行sudo docker xxx命令,会提示输入当前账号密码,然后可以成功:
willzhao@ideapad:~$ sudo docker images
[sudo] willzhao 的密码:
REPOSITORY TAG IMAGE ID CREATED SIZE
bolingcavalry/probedemo 0.0.1 803f83e12d88 3 hours ago 508MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
openjdk 8u212-jdk-stretch 03b20c1fa768 11 months ago 488MB
- 每次执行docker命令都要输入密码不方便,尤其是在shell脚本中就更麻烦了,一起来解决这个问题;
设置
- 创建名为docker的组,如果之前已经有该组就会报错,可以忽略这个错误:
sudo groupadd docker
- 将当前用户加入组docker:
sudo gpasswd -a ${USER} docker
- 重启docker服务(生产环境请慎用):
sudo systemctl restart docker
- 添加访问和执行权限:
sudo chmod a+rw /var/run/docker.sock
- 操作完毕,验证一下,现在可以不用带sudo了:
willzhao@ideapad:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bolingcavalry/probedemo 0.0.1 803f83e12d88 4 hours ago 508MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
openjdk 8u212-jdk-stretch 03b20c1fa768 11 months ago 488MB