docker - 如何获取 docker 镜像中使用的用户 uid 和名称
问题描述
我有自定义用户(例如 tod)的 docker 图像。我想在不创建容器的情况下查找有关此用户的信息。
基础镜像:centos8
UPD:关于上下文。我在 k8s 中运行一个非特权容器并收到以下错误:
container has runAsNonRoot and image has non-numeric user (tod), cannot verify user is non-root
我读了这个答案,不明白为什么不能从我的容器中获取用户 ID。
解决方案
让我们尝试分析以下代码:
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 和用户名是互斥的。看看这些参数的描述:
// 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"`
所以事实证明这不是一个错误。这就是容器运行时接口标准的设计方式,您对此无能为力。
你可以做的是改变你在 Dockerfile 中使用 USER 指令的方式。
不要使用带有 USER 指令的用户名,而是创建一个具有已知 uid 的用户并改用此 uid,如下例所示:
RUN useradd -r -u 1001 appuser
USER 1001
推荐阅读
- machine-learning - 有没有好的开源图像数据集生成软件?
- git - 使用 GitHub 存储库和 SSH 登录的 Spring Boot 配置服务器
- android - 在颤动中选择时,BottomNavigationBarItem 标签不显示
- akka - 在 Akka Typed Cluster Sharding 中,持久化 EntityRef 以供将来使用是否安全?
- python - 未找到隧道错误 Pyngrok streamlit collab
- python-3.x - 刮板无法突然运行并且显示驱动程序未定义
- swift - 使用大标题时的部分搜索更新框架
- java - 如何解决`遇到
在第 1 行 ...` 问题 - java - Files.probeContentType() 在 OpenJDK 11 上返回 null
- mysql - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 。安装后出错