Home > Back-end >  Creating user with uid 1000 on alpine Docker image
Creating user with uid 1000 on alpine Docker image

Time:11-21

When I'm trying to create user with UID=1000 in Dockerfile

FROM alpine:3.16

RUN addgroup -g 1000 sail
RUN adduser -g 1000 -u 1000 sail

I'm getting error:

Step 3/3 : RUN adduser -g 1000 -u 1000 sail
 ---> Running in 0c4ce7e0bddf
adduser: uid '1000' in use
The command '/bin/sh -c adduser -g 1000 -u 1000 sail' returned a non-zero code: 1

But if I'm build a container without this stage and look to /etc/passwd there is no user with UID=1000. So it's exists only while building.

How to create user with UID=1000 properly in alpine?

CodePudding user response:

Your syntax is incorrect, the adduser command of Alpine is the BusyBox one, so, unlike the "regular" adduser, here are its help page:

BusyBox v1.35.0 (2022-08-01 15:14:44 UTC) multi-call binary.

Usage: adduser [OPTIONS] USER [GROUP]

Create new user, or add USER to GROUP

        -h DIR          Home directory
        -g GECOS        GECOS field
        -s SHELL        Login shell
        -G GRP          Group
        -S              Create a system user
        -D              Don't assign a password
        -H              Don't create home directory
        -u UID          User id
        -k SKEL         Skeleton directory (/etc/skel)

You can easily go through it, running the command:

docker run -ti --rm alpine:3.16 adduser

The important part of information here are:

-g GECOS        GECOS field
-G GRP          Group

Where you can see that, a group, in BusyBox adduser requires your to be added with the option G, in capital letter, and that the option g in lowercase if for something else.

The option allows you to add a group, and not a GID, so you'll need the command:

RUN adduser -G sail -u 1000 sail

Furthermore, if you run that command, the shell will prompt you to fill in a password. You will need to skip this with the D option:

-D              Don't assign a password

And so, your Dockerfile ends up being:

FROM alpine:3.16

RUN addgroup -g 1000 sail \
    && adduser -G sail -u 1000 sail -D

Note that it is always a good idea, in Docker, to reduce as much as possible the number of layers you are creating by running subsequent RUN instruction, for further reading on this, see here.

  • Related