Consider the following trivial Dockerfile:
FROM debian:testing
RUN adduser --disabled-password --gecos \'\' docker
RUN adduser --disabled-password --gecos
So, I ended up in this post looking on how to restore ownership of all the files (owned by root) that came out of a docker container running as root, to my non-privileged user in the host.
I needed the process inside the container to run as root, so I can't use -u on docker run.
I'm not proud of what I did, but at the end of my bash script, I added this:
docker run --rm -it \
--entrypoint /bin/sh \
-e HOST_UID=`id -u` \
-v ${HOST_FOLDER_OWNED_BY_ROOT}:/tmp \
alpine:latest \
-c 'chown -R ${HOST_UID}:${HOST_UID} /tmp/'
Let's break some of the lines down:
--entrypoint /bin/sh
-e HOST_UID=`id -u`
/tmp:-v ${HOST_FOLDER_OWNED_BY_ROOT}:/tmp
chown recursively with the host user's uid over the target directory (mounted inside the container in /tmp):-c 'chown -R ${HOST_UID}:${HOST_UID} /tmp/'
So, with this, I got the files owned back to my current user without having to "escalate" privileges to root or to sudo.
It's dirty, but it worked. Hope I helped.