0

I am trying to create new user in my docker image:

FROM openjdk:8u252-jre-buster

WORKDIR /myapplication

RUN groupadd -r -g 1000 agroup
RUN echo $?
RUN getent group agroup
RUN su -c 'useradd --system --shell /sbin/nologin --uid 1000 --gid agroup appuser --root /myapplication'
RUN echo $?
RUN chown 1000:1000 -cR /myapplication
RUN echo $?
USER 1000

this throws error:

useradd: group 'agroup' does not exist

The command '/bin/sh -c su -c 'useradd --system --shell /sbin/nologin --uid 1000 --gid agroup appuser --root /myapplication'' returned a non-zero code: 6

If I remove --root /myapplication from the useradd command, all passes through, what additionally I need to set in order to pass with --root parameter?

The provided example run with docker build . produces quoted output.

2
  • Have you tried to "su -" your group creation? It's been a while since I last had to add users and user groups in a Linux server, but I seem to remember this requires elevated rights.
    – user1019780
    Apr 23, 2020 at 7:45
  • In Debian, you should use the adduser instead of the useradd command. It makes things much simpler! May 26, 2020 at 14:45

1 Answer 1

0

The first command creates a group in a non-chroot environment (/etc/group), while the useradd command creates a user in a chroot environment (/myapplication). In general, that wouldn't be a problem if it wasn't because you are trying to add the user to the group from the first command, but this doesn't exist in the chroot environment.

There are two possible solutions to this problem. Either you create the group in the chroot environment:

groupadd -r -g 1000 agroup --root /myapplication

Or the user is created outside the chroot environment:

su -c 'useradd --system --shell /sbin/nologin --uid 1000 --gid agroup appuser'

I don't know what you are trying to do so I can't tell you which of the two solutions is correct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .