264

I'm using alpine (or an image that is based on Alpine) as the base image in my Dockerfile. Which instructions do I need to add to create a user?

Eventually I'll use this user to run the application I'll place into the container so that the root user does not.

4 Answers 4

456

Alpine uses the command adduser and addgroup for creating users and groups (rather than useradd and usergroup).

FROM alpine:latest

# Create a group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Tell docker that all future commands should run as the appuser user
USER appuser

The flags for adduser are:

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)

Add new user official docs

8
  • 13
    Or alternatively, you can replace the whole snippet above using this: USER 405 which is the guest user within Alpine Linux. Jun 2, 2018 at 12:14
  • 14
    Why not USER guest?
    – user672009
    Sep 26, 2018 at 22:22
  • 3
    I'd go with creating a new user because I want that user to have the same UID/GID as the one on the host OS, so that there's no permission issue when running docker in Linux. (not an issue with macOS/Windows users)
    – elquimista
    Oct 17, 2018 at 18:29
  • 18
    Note that since Alpine is based on BusyBox, its adduser and addgroup commands are different from adduser and addgroup as provided by Debian and Ubuntu which in turn are front ends to useradd and groupadd. Notably, the Debian and Ubuntu commands only support long form options. See: manpages.debian.org/stretch/adduser/adduser.8.en.html
    – Jack
    Mar 3, 2019 at 3:43
  • 4
    What's a "system" user?
    – xpmatteo
    Dec 30, 2021 at 13:20
154

The commands are adduser and addgroup.

Here's a template for Docker you can use in busybox environments (alpine) as well as Debian-based environments (Ubuntu, etc.):

ENV USER=docker
ENV UID=12345
ENV GID=23456

RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "$(pwd)" \
    --ingroup "$USER" \
    --no-create-home \
    --uid "$UID" \
    "$USER"

Note the following:

  • --disabled-password prevents prompt for a password
  • --gecos "" circumvents the prompt for "Full Name" etc. on Debian-based systems
  • --home "$(pwd)" sets the user's home to the WORKDIR. You may not want this.
  • --no-create-home prevents cruft getting copied into the directory from /etc/skel

The usage description for these applications is missing the long flags present in the code for adduser and addgroup.

The following long-form flags should work both in alpine as well as debian-derivatives:

adduser

BusyBox v1.28.4 (2018-05-30 10:45:57 UTC) multi-call binary.

Usage: adduser [OPTIONS] USER [GROUP]

Create new user, or add USER to GROUP

        --home DIR           Home directory
        --gecos GECOS        GECOS field
        --shell SHELL        Login shell
        --ingroup GRP        Group (by name)
        --system             Create a system user
        --disabled-password  Don't assign a password
        --no-create-home     Don't create home directory
        --uid UID            User id

One thing to note is that if --ingroup isn't set then the GID is assigned to match the UID. If the GID corresponding to the provided UID already exists adduser will fail.

addgroup

BusyBox v1.28.4 (2018-05-30 10:45:57 UTC) multi-call binary.

Usage: addgroup [-g GID] [-S] [USER] GROUP

Add a group or add a user to a group

        --gid GID  Group id
        --system   Create a system group

I discovered all of this while trying to write my own alternative to the fixuid project for running containers as the hosts UID/GID.

My entrypoint helper script can be found on GitHub.

The intent is to prepend that script as the first argument to ENTRYPOINT which should cause Docker to infer UID and GID from a relevant bind mount.

An environment variable "TEMPLATE" may be required to determine where the permissions should be inferred from.

(At the time of writing I don't have documentation for my script. It's still on the todo list!!)

7
  • 29
    +1, using long form for command args increases readability and makes maintenance easier. When writing shell scripts always use the long form (Dockerfile RUN is nothing else than a shell script). Sep 5, 2019 at 20:25
  • Great answer, and thank for sharing your script. I am interested in retaining the host uid/gid: on debian based images my approach consisted in passing these over through env variables or infer from a bound workspace folder. All this was made easier by using commands useradd/groupadd with the --non-unique flag. Is there anyway to create duplicate id users/groups in alpine?
    – n1nsa1d00
    Jul 1, 2021 at 10:37
  • 2
    I found an answer to my question: installing shadow.
    – n1nsa1d00
    Jul 1, 2021 at 11:22
  • 1
    Isn't --ingroup "$USER" supposed to be --ingroup "$GID"? I'm confused. Apr 10, 2022 at 6:34
  • 1
    @Thegerdfather you're right. Without that, the build will fail (and $GID would be useless). The group needs to exist already, too
    – TBG
    May 21 at 14:33
14

There is package shadow that brings useradd & usermod.

adduser has some stupid limitations:

$ sudo adduser --disabled-password root
adduser: user 'root' in use

but usermod doesn't:

$ sudo apk add shadow
$ sudo usermod --unlock root
1
  • 1
    shadow also includes groupadd too and makes alpine consistent in this regard with other distros so if you have a multi distro script (e.g. docker) using these then just install shadow when spinning up an alpine container and your script will work anywhere.
    – DKebler
    Apr 13 at 5:35
1

Group and User Setup:

Create default values for User ID (UID), Group ID (GID), and username, which can be customized using arguments.

ARG USER_UID=1000
ARG GROUP_GID=1000
ARG UGNAME=webapp

Remove Existing Group and User:

Check for the presence of an existing group and user. If either exists, remove them.

RUN if getent passwd ${USER_UID} >/dev/null; then \
    deluser $(getent passwd ${USER_UID} | cut -d: -f1); fi

RUN if getent group ${GROUP_GID} >/dev/null; then \
    delgroup $(getent group ${GROUP_GID} | cut -d: -f1); fi

Create New Group and User:

Create a new system group and a new system user with the specified UID, GID, and username.

RUN addgroup --system --gid ${GROUP_GID} ${UGNAME}

RUN adduser --system --disabled-password --home /home/${UGNAME} \
    --uid ${USER_UID} --ingroup ${UGNAME} ${UGNAME}

Set the user as the default user for the container. All subsequent commands will run with this user.

USER ${UGNAME}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.