【问题标题】:How to get user uid and name used in docker image如何获取 docker 镜像中使用的用户 uid 和名称
【发布时间】:2021-05-26 16:40:51
【问题描述】:

我有自定义用户(例如 tod)的 docker 镜像。我想在不创建容器的情况下找出有关此用户的信息。

基础镜像:centos8

UPD:关于上下文。 我在 k8s 中运行一个非特权容器并收到以下错误: container has runAsNonRoot and image has non-numeric user (tod), cannot verify user is non-root

我阅读了this 的答案,无法理解为什么无法从我的容器中获取用户 ID。

【问题讨论】:

  • 为什么会有特定的“不创建容器”约束? docker run --rm imagename id 会经常工作,但在技术上违反了这个要求。
  • 这张图片被k8s检查了,找不到用户id。但如果你去容器,用户有一个 id。魔法)
  • 您能否在问题中添加更多详细信息,以了解您在哪种情况下要检查的具体内容? (每个进程都会有 一些 用户 ID;您可以在 docker run 和 Kubernetes Pod 规范中覆盖容器的 uid;通常将 Kubernetes 配置为不允许进程以 root 身份运行;这种情况不常见但可能容器以 root 身份启动,然后切换到非特权用户。)
  • 我添加了上下文
  • 这是解释这一点的 PR:github.com/kubernetes/kubernetes/pull/56503 这能回答你的问题吗?

标签: docker kubernetes


【解决方案1】:

让我们尝试分析以下代码:

github code reference link

case uid == nil && len(username) > 0:
    return fmt.Errorf("container has runAsNonRoot and image has non-numeric user (%s), cannot verify user is non-root (pod: %q, container: %s)", username, format.Pod(pod), container.Name)

这是打印您看到的错误的代码。您看到错误是因为uid == nil 和同时username != ""

但是为什么 username 有值而 uid 没有呢?为什么它们都没有价值?

事实证明他们不能,因为 UID 和用户名是互斥的。看看这些参数的说明:

github code reference link:

// UID that will run the command(s). This is used as a default if no user is
// specified when creating the container. UID and the following user name
// are mutually exclusive.
Uid *Int64Value `protobuf:"bytes,5,opt,name=uid,proto3" json:"uid,omitempty"`
// User name that will run the command(s). This is used if UID is not set
// and no user is specified when creating container.
Username string `protobuf:"bytes,6,opt,name=username,proto3" json:"username,omitempty"`

所以事实证明这不是一个错误。这就是容器运行时接口标准的设计方式,您对此无能为力。


你可以做的是改变你使用USER instruction in Dockerfile的方式。

不要在 USER 指令中使用用户名,而是创建一个具有已知 uid 的用户并改用此 uid,如下例所示:

RUN useradd -r -u 1001 appuser
USER 1001

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多