大网数据广告活动
服务器运维问答

How to manage bind mount ownership and permissions for containers running with non-root user?

· 52CE 我爱测

We are using Red Hat container images on RHEL hosts, all based on their ubi7 or ubi8 images, which by default, run as the default user (uid 1001). This poses a challenge when a container needs write access to a host directory (i.…

问题原文

原帖链接 · Yanick Girouard · CC BY-SA 4.0

We are using Red Hat container images on RHEL hosts, all based on their ubi7 or ubi8 images, which by default, run as the default user (uid 1001).

This poses a challenge when a container needs write access to a host directory (i.e. to write logs or temporary files), because the UID and GID of the host is preserved in the container, and files written by the container will be owned by the UID of the container user.

This leaves me with two choices as far as I can tell:

  1. Create a user on my host with UID 1001 and set that user as the owner of the mounted volume. This may pose problems because UID 1001 is the first UID that will be used when adding a new user on a host where the UID is not forced to something specific. Therefore, it can be hard to manage if the container needs to be deployed on multiple servers that may or may not have the same consistent UID mappings. This would however be the only way as far as I can tell that the owner of files written by the container (with UID 1001) match the desired user on the host, and that the owner of files written on the host match an existing UID in the container.
  2. Make the mounted host directory as world-writable, which comes with many security implications, one of which is that any user on the host would have access to delete files written by the container. The files could also show as being owned by another user if UID 1001 is already assigned to another user on the host.

Normally, I would just create a matching user:group combo inside the container image, reset the ownership and permissions where needed and rebuild it, but for the images that Red Hat creates, this implies a lot of leg work as everything is made to be run with UID 1001 and there are many scripts (container-entrypoint fix-permissions, generate-container-user) that force it that way.

Am I understanding everything correctly or is there another (better) method for doing this?

已采纳解答

原帖链接 · Yanick Girouard · CC BY-SA 4.0

I just figured it out... The Red Hat images automatically change the uid of the default user to whatever you run the container as using the generate-container-user script, so I just need to finish my Dockerfile with either USER , or run it with --user and that works.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。