How to create a user in Dockerfile

Categories:

Debian and Ubuntu

Dockerfile
1RUN set -eu;\
2    useradd --user-group --create-home --comment="$comment" "$username";\
3    yes "$password" | passwd "$username"

Alpine

Dockerfile
1RUN yes "$password" | adduser --gecos "$comment" "$username"

Variables

  • $username — username;
  • $password — password (can be equal to username for convenience);
  • $comment — comment (GECOS field).

Documentation