Variables in Dockerfile don't seem to be recognized?

I am building an image using Dockfile. I would like to set the Username of the container via the command line to avoid permission issues.

The Dockfile is shown below, I used the variables of USER_NAME, GROUP_ID. But when I build, the problem keeps appearing.
The error is: groupadd: option '--gid' requires an argument
I’m guessing that both ${GROUP_ID} and ${USER_NAME} are recognized as empty strings, but shouldn’t they be assigned values ​​when the container is created?
I’ve googled a few examples and based on the examples, I don’t quite see where the problem is?

Please help me!
Thanks!

FROM matthewfeickert/docker-python3-ubuntu:latest
ARG USER_NAME
ARG USER_ID
ARG GROUP_ID


RUN groupadd -r --gid ${GROUP_ID} ${USER_NAME} 
RUN useradd --no-log-init -r -g ${GROUP_ID} -u ${USER_ID} ${USER_NAME}

USER ${USER_NAME}
WORKDIR /usr/local/src

>Solution :

You can pass the build args as shown below.

docker build --build-arg USER_NAME=test --build-arg USER_ID=805 --build-arg GROUP_ID=805 -t tag1 .

Also, as a best practice consider adding default vales to the args. So if the user doesn’t specify the args the default values will be picked.

Leave a Reply