如何获取docker镜像中使用的用户uid和名称



我有自定义用户的docker映像(例如tod)。我想在不创建容器的情况下找到这个用户的信息。

基本图片:centos8

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

我读了这个答案,不明白为什么不能从我的容器中获得用户id。

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

github代码参考链接

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 == nilusername != ""同时出现

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

结果是他们不能,因为UID和用户名是互斥的。看一下这些参数的说明:

github代码参考链接:

// 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"`

所以这不是一个bug。这就是容器运行时接口标准的设计方式,你对此无能为力。


你能做的就是改变你在Dockerfile中使用USER指令的方式。

不要在USER指令中使用username,创建一个uid已知的用户,并使用这个uid,如下例所示:

RUN useradd -r -u 1001 appuser
USER 1001

最新更新