New to docker as you noticed. I'm trying to build a docker image with me assigning the current GID/UID to the usermod/groupmod command that I will run inside the docker image.
I tried the command with environement variables but It didnt work
the 1002 and 1003 is the user GID/UID of the connected user
FROM mongo:4.4.9
# setup folder before switching to user
RUN mkdir /var/lib/mongo
RUN usermod -u 1002 mongodb
RUN groupmod -g 1003 mongodb
RUN chown mongodb:mongodb /var/lib/mongo
RUN chown mongodb:mongodb /var/log/mongodb
USER mongodb
I tried this :
FROM mongo:4.4.9
ENV MONGO_UID=$MONGO_UID
ENV MONGO_GID=$MONGO_GID
# setup folder before switching to user
RUN mkdir /var/lib/mongo
RUN usermod -u $MONGO_UID mongodb
RUN groupmod -g $MONGO_GID mongodb
RUN chown mongodb:mongodb /var/lib/mongo
RUN chown mongodb:mongodb /var/log/mongodb
USER mongodb
and building by this :
docker build --build-arg MONGO_UID=1002 --build-arg MONGO_GID=1003 .
Thanks for any help !
CodePudding user response:
You need to declare them as an argument and not as an environment variable, since you are passing an argument in the docker build
command.
The Dockerfile
should be something like this:
FROM mongo:4.4.9
ARG MONGO_UID
ARG MONGO_GID
# setup folder before switching to user
RUN mkdir /var/lib/mongo
RUN usermod -u ${MONGO_UID} mongodb
RUN groupmod -g ${MONGO_GID} mongodb
RUN chown mongodb:mongodb /var/lib/mongo
RUN chown mongodb:mongodb /var/log/mongodb
USER mongodb
If you need to have them as an environment variable inside the container, then you would need to say that the ENV
is equal to the ARG
:
ARG MONGO_UID
ENV MONGO_UID=${MONGO_UID}
It's also possible to give the argument
(same to ENV
) a default value
, in case you are interested:
ARG MONGO_UID=default
Not having a default value will force the user to supply a value to the argument.