Dockerfile "useradd: user already exists" in CI
A RUN useradd or RUN groupadd failed because the name already exists. Many language base images (node, postgres, www-data) ship a predefined user, so re-creating it aborts the step.
What this error means
A RUN step fails with "useradd: user 'node' already exists" or "groupadd: group 'app' already exists" (Debian adduser prints "The user 'X' already exists. Exiting.").
useradd: user 'node' already exists
The command '/bin/sh -c useradd -m node' returned a non-zero code: 1Common causes
The base image predefines the user or group
Images like node already include a node user and group, so a second useradd node collides with the existing entry.
A duplicate UID or GID collision
Re-using an in-use UID or GID triggers a related failure such as "useradd: UID N is not unique" or "addgroup: gid N in use".
How to fix it
Guard creation so it is idempotent
Only create the user or group when it is not already present.
RUN getent group app || groupadd app
RUN id -u app >/dev/null 2>&1 || useradd -g app appReuse the predefined account
- Check the base image docs for an existing non-root user.
- Switch to it with
USER nodeinstead of creating a new one. - Only add a user when the image truly has none.
USER nodeHow to prevent it
- Check whether the base image already ships a non-root user.
- Guard
useradd/groupaddwith an existence check. - Pick a non-colliding name and UID when you must add one.