Reputation: 410
I can add a user to an docker image using Dockerfile with following command:
RUN useradd -ms /bin/bash [username]
USER [username]
WORKDIR /home/[username]
However, If I want to add more than one user to an image with following command, I encounter with an error for second user:
RUN useradd -ms /bin/bash [username1]
USER [username1]
WORKDIR /home/[username1]
RUN useradd -ms /bin/bash [username2]
USER [username2]
WORKDIR /home/[username2]
Error message is:
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
Upvotes: 2
Views: 2248
Reputation: 1673
This is because second useradd
command is running as username1 which does not have sufficient privileges to add user
Moving the second useradd before USER [username1]
would make it work for you
RUN useradd -ms /bin/bash username1
RUN useradd -ms /bin/bash username2
USER username1
WORKDIR /home/username1
USER username2
WORKDIR /home/username2
Upvotes: 5