Add user in Docker container, UID mismatch when running Jenkins job

  docker, jenkins, jenkins-pipeline

I am running a Jenkins pipeline in a Docker container. The Docker container creates an unpriviliged user to run as:

RUN useradd jenkins --shell /bin/bash --create-home
RUN mkdir -p /home/jenkins/src && chown -R jenkins:jenkins /home/jenkins
USER jenkins
WORKDIR /home/jenkins/src

Jenkins runs this as:

docker run -t -d -u 1000:1000 [-v and -e flags etc.]

This works when I run Jenkins manually as my personal account (uid 1000) on the host. But now I changed it so that Jenkins is started automatically by systemd, and using a specifiy jenkins user with uid 1006, gid 1009:

docker run -t -d -u 1006:1009 [-v and -e flags etc.]

This mismatch causes my build to fail. I also get all kinds of problems, like this prompt in the container:

I have no [email protected]:/$

Creating a jenkins user in the container seems like something that there should be a recipe for. How do I get the UIDs on host and container to match? What is the best practice?

  • Add something like usermod --uid $HOST_UID jenkins to the Dockerfile?
  • There seems to be no way to tell Docker to map host uid 1006 to container uid 1000, is there?

Source: Docker Questions