For the special case where you want to share a base image between production/development without rebuilding it on every machine with a new developer user ID/GID, see the article Running Docker Containers as Current Host User - Juan Treminio. To summarize, recreate your internal docker user in a new Dockerfile:
FROM jtreminio/php:7.2
ARG USER_ID
ARG GROUP_ID
RUN if [ ${USER_ID:-0} -ne 0 ] && [ ${GROUP_ID:-0} -ne 0 ]; then \
userdel -f www-data &&\
if getent group www-data ; then groupdel www-data; fi &&\
groupadd -g ${GROUP_ID} www-data &&\
useradd -l -u ${USER_ID} -g www-data www-data &&\
install -d -m 0755 -o www-data -g www-data /home/www-data &&\
chown --changes --silent --no-dereference --recursive \
--from=33:33 ${USER_ID}:${GROUP_ID} \
/home/www-data \
/.composer \
/var/run/php-fpm \
/var/lib/php/sessions \
;fi
USER www-data
For the even more special case where you've limited the creation of any of the internal docker user's files to the home directory, you can let usermod
do the chown
operation for you. From the usermod
man page:
-u, --uid UID
The new numerical value of the user's ID.
...
The user's mailbox, and any files which the user owns and which are located in the user's home directory will have the file user ID changed automatically.
The ownership of files outside of the user's home directory must be fixed manually.
Juan's solution then simplifies to something like:
USER root
ARG HOST_ID="1000"
ARG HOST_GID="1000"
RUN if [ $HOST_ID -ne $USER_ID ] || [ $HOST_GID -ne $USER_GID ]; then \
groupadd --gid "$HOST_GID" "host_user_group" &&\
usermod --uid "$HOST_ID" --gid "$HOST_GID" "$UNAME"\
;fi
USER $UNAME