How to map the same user from host to container in podman ?

Solution In Progress - Updated -

Environment

  • Red Hat Enterprise Linux 8
  • Red Hat Enterprise Linux 9
  • podman

Issue

  • Mapping the same user on host to a container so that the files owned on the host can be mapped inside the container and can be accessed with ease

Resolution

  • One can make use of the flag --userns keep-id when creating a container. This will make sure to map user account to same UID within container
  • Below are test results from a sample environment
[user@vm252-105 ~]$ mkdir host

[user@vm252-105 ~]$ touch host/testfile

[user@vm252-105 ~]$ echo "Hellooooo" > host/testfile 

[user@vm252-105 ~]$ ls -l host/testfile 
-rw-r--r--. 1 user user 10 Jul 31 04:01 host/testfile

[user@vm252-105 ~]$ whoami
user

[user@vm252-105 ~]$ podman run -itd --userns keep-id -v ./host/:/tmp/container:Z ubi8
4bb7fc5bd4f166f86edc3b30226c306489557c8dd4e142c8ca3edbb3df539bc0

[user@vm252-105 ~]$ podman exec -itl bash
bash-4.4$ whoami
user

bash-4.4$ cd /tmp/container/

bash-4.4$ ls -l
total 4
-rw-r--r--. 1 user user 10 Jul 31 09:01 testfile

bash-4.4$ cat testfile 
Hellooooo
  • Note that the it is not mandatory to have the user already present in the container. podman will take care of creating it when the flag is passed

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments