I wrote a Docker image which need to read some variables so I wrote in the Dockerfile
:
ARG UID=1000
ARG GID=1000
ENV UID=${UID}
ENV GID=${GID}
ENV USER=laravel
RUN useradd -G www-data,root -u $UID -d /home/$USER $USER
RUN mkdir -p /home/$USER/.composer && \
chown -R $USER:$USER /home/$USER
This code actually allow me to create a laravel
user which has the id of the user that starts the container.
So an user that pull this image actually set in the docker-compose
section this content:
env_file: .env
which have:
GROUP_ID=1001
USER_ID=1001
For some weird reason that I don't understand, when I exec in the container with the pulled image, the user laravel
is mapped with the id 1000 which that is the default value setted in the Dockerfile
.
Instead, if I test the image using:
build:
context: ./docker/php
dockerfile: ./Dockerfile
args:
- UID=${GROUP_ID:-1000}
- GID=${USER_ID:-1000}
I can see correctly the user laravel
mapped as 1001
. So the questions are the following:
- is the UID variable not reading from env file?
- is the default value overwriting the env value?
Thanks in advance for any help
UPDATE:
As suggested, I tried to change the user id and group id in the bash script executed in the entrypoint, in the Dockerfile
I have this:
ENTRYPOINT ["start.sh"]
then, at the start of start.sh
I've added:
usermod -u ${USER_ID} laravel
groupmod -g ${GROUP_ID} laravel
the issue now is:
usermod: user laravel is currently used by process 1 groupmod: Permission denied. groupmod: cannot lock /etc/group; try again later.
CodePudding user response:
Docker build phase
and the run phase
are the key moment here. New user is added in the build phase
and hence it is important to pass dynamic values while building docker image in build phase
with e.g.
docker build --build-arg UID=1001 --build-arg GID=1001 .
or the case which you have already used and where it works (i.e. docker image is re-created with expected IDs), in docker-compose
file:
build:
context: ./docker/php
dockerfile: ./Dockerfile
args:
- UID=${GROUP_ID:-1000}
- GID=${USER_ID:-1000}
In run phase
, i.e. starting the container instance of already built docker image, passing env
does not overwrite vars of build phase
. Hence, in you case you can omit passing envs when starting container.