Bind Mount Permissions – Managing Bind Mount Ownership for Non-Root Containers

dockerlinuxmount

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?

Best Answer

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.