Docker.dotnet ListImages 在 Redhat 中的 /var/run/docker.sock 上因权限被拒绝而失败

问题描述

免责声明我知道 /var/run/docker.sock 问题很常见,并且有很多关于它的帖子(尽管大多数(如果不是全部)都可以总结为添加运行用户到 docker 权限组)。我尝试了所有这些说明,但在 redhat 中仍然对我没有帮助。

我有两个容器,一个 Ubuntu一个运行 Redhat 7.9。 我的问题特别是无法运行 - 仅在 redhat 容器中 - 对 Docker.Dotnet's Listimages调用(因 /var/run/docker.sock 中的权限被拒绝而失败)。一开始,我无法在不使用 sudo 前缀的情况下发出任何 docker 命令。然后我将运行用户添加到 docker 权限组,并且可以在没有 sudo 的情况下发出 docker 命令。 但是 Docker.Dotnet Listimages(它只是 docker api 的图像/json 端点的包装器)仍然失败,并在 docker.sock 上出现权限被拒绝错误。我尝试了所有推荐的here,但无济于事。

我想也许我应该添加 User=root (虽然这在我的 Ubuntu 服务文件中不存在,因此没有多大意义)。然后我意识到 ubuntu 和 redhat docker 服务文件有很大不同。

Ubuntu

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd://
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process,not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target 

红帽

[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.service registries.service
Wants=docker-storage-setup.service
Requires=rhel-push-plugin.service registries.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=main
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --authorization-plugin=rhel-push-plugin \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
          $REGISTRIES
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process

[Install]
WantedBy=multi-user.target

更新: 在 ubuntu 中,我可以使用 curl --unix-socket /run/docker.sock http://docker/images/json 从应用容器之一中成功运行 docker engine api 命令。在 redhat 中执行相同操作返回 curl: (7) Couldn't connect to server

这里有什么进一步的指导吗? redhat 授权插件看起来很可疑...

解决方法

最后……我的问题是在我的 Redhat 安装中,与我的 Ubuntu 不同,我们启用了 SELinux

Disabling it 终于让 curl --unix-socket /run/docker.sock http://docker/images/json 在我的 Composer 容器中工作。

要禁用 Selinux:编辑(您可能需要使用 sudo su root 模拟为 root)文件 /etc/selinux/config - 将 SELINUX=enforcing 替换为 SELINUX=disabled

重启 linux 服务器就可以了。

备注:这显然不是生产环境中可接受的解决方案。如果是这种情况,则需要正确配置 SELinux 权限设置。我只是被分配了一项任务来确定为什么我们的一台开发机器上会发生这个问题,所以暂时禁用它就足够了。