Reputation: 12462
Dockerfile
FROM ubuntu
ENV UID "1234"
ENV USERNAME "default"
RUN addgroup $USERNAME && useradd -u $UID -g $USERNAME -ms /bin/bash $USERNAME
CMD ["/usr/bin/id", "-u", "myname"]
and running docker via:
docker build -t setuser .
docker run -e UID=5555 -e USERNAME=myname setuser
OUTPUT
/usr/bin/id: 'myname': no such user
this error tells me that useradd
in Dockerfile was not executed during "docker run" time.
how could I achieve this?
CMD ["/usr/bin/id", "-u", "default"]
works but i want to be able to supply the UID and USERNAME at run time, not at Dockerfile build time
Upvotes: 1
Views: 1658
Reputation: 13804
To elaborate Henry's answer
Following portion of your Dockerfile executed when you build your image.
FROM ubuntu
ENV UID "1234"
ENV USERNAME "default"
RUN addgroup $USERNAME && useradd -u $UID -g $USERNAME -ms /bin/bash $USERNAME
So, this time, UID=1234
& USERNAME=default
are set.
When you run your image, It starts with ENTRYPOINT & CMD. It has no effect on RUN addgroup $USERNAME ...
So you need to run a script to use your passing ENV.
#!/bin/bash
addgroup $USERNAME && useradd -u $UID -g $USERNAME -ms /bin/bash $USERNAME
id -u $USERNAME
Here, your passing ENV values will be used.
Upvotes: 5
Reputation: 43798
The useradd was executed when the image was created. If you want to run some commands when the container is started put them into a script that is executed at start time.
Upvotes: 1