Docker Community Forums

Share and learn in the Docker community.

Docker + Alpine Linux + Unknown User + Su

Hello,
I would like run a container with an unknown user (by --user UID) but i need to create this user at runtime

Dockerfile:

FROM alpine:latest
RUN /bin/echo 'root:root' | chpasswd
COPY ./entrypoint.sh /
ENTRYPOINT [ "/bin/sh", "/entrypoint.sh" ]

entrypoint.sh:

#!/bin/sh
su - root -c "adduser -u $(id -u) unknown"
exec "$@"

But the binary “su” gives me the error:

su : Cannot determine your user name.

Do you have any idea so that I can counter this error please?

The su-exec can be used in alpine. Do add it the package, if not already available, add the following to your Dockerfile

RUN apk add --no-cache su-exec
Inside your scripts you’d run inside docker you can use the following to become another user:

exec su-exec
Alternatively, you could add the more familiair sudo package while building your docker-file Add the following to your Dockerfile that’s FROM alpine

RUN set -ex && apk --no-cache add sudo
After that you can use sudo

sudo -u

Thank you for your help but
With su-exec root adduser[…] i got error: “su-exec: setgroups: Operation not permitted”
With sudo -u root adduser[…] i got error: “sudo: you do not exist in the passwd database”