zoukankan      html  css  js  c++  java
  • 【亲测有效】运行docker ps 出现Got permission denied问题的解决方案

    问题描述

    今天在运行 docker ps 命令的时候出现如下问题:

    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.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied
    

    看提示信息好像是权限不够,我们试试 root 权限:

    我们发现用 root 权限就可以使用 docker 相关命令,那我们想在普通用户下使用 docker 相关命令,这该怎么办呢?

    我们去 Docker Mannual 找到原因,原因如下:

    Manage Docker as a non-root user
    
    The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
    
    If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
    

    上面这段话的意思是 docker 进程使用 Unix Socket 而不是 TCP 端口。而默认情况下, Unix socket 属于 root 用户,需要 root 权限才能访问。

    我们该怎么解决这个问题呢?

    解决方案一

    使用 sudo 获取管理员权限,运行 docker 命令(当然我不推荐这种方法,因为我没成功过,似乎还是出现了如上问题)

    解决方案二

    由于 docker 守护进程启动的时候,会默认赋予名字为 docker 的用户组读写 Unix socket 的权限,因此只要创建 docker 用户组,并将当前用户加入到 docker 用户组中,那么当前用户就有权限访问 Unix socket 了,进而也就可以执行 docker 相关命令了。

    我们可以使用如下命令解决问题:

    sudo groupadd docker     #添加docker用户组
    sudo gpasswd -a $USER docker     #将登陆用户加入到docker用户组中
    newgrp docker     #更新用户组
    docker ps    #测试docker命令是否可以使用sudo正常使用
    

    然后我们可以完美地解决了这个问题,效果如下:

  • 相关阅读:
    Codeforces ECR 83 C. Adding Powers (位运算)
    Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)
    Codeforces Round #603 C. Everyone is a Winner!
    Centos7 下搭建SVN + Apache 服务器 风行天下
    完整部署CentOS7.2+OpenStack+kvm 云平台环境(1)基础环境搭建 风行天下
    云计算的理解 风行天下
    Python之路3【知识点】白话Python编码和文件操作 风行天下
    C#中TreeView组件使用方法初步
    复制文件时explorer.exe出错解决方法
    C# 里TreeView绑定数据库实现分类
  • 原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/9889958.html
Copyright © 2011-2022 走看看