Oftentimes, applications insider docker containers are either run by root or some other user set up with some specific user and group ID.
However, sometimes it can be useful to have your application inside a container run by a user with the same user and group ID as the host's user. For example when you bind-mount scripts or secret files into your container that can be only executed / read by the host user. Or when you bind-mount volumes into your container and you do not want to have to use sudo on your host to e.g. remove files generated by the root user inside the container.
By default, the Jenkins docker container is set up with a user called jenkins with user and group ID 1000. In this post we will set up a docker-compose.yml file that creates a Jenkins server that is based on the official jenkins/jenkins:lts docker image and that changes the default uid and gid of the jenkins user to that of the host's user.
TLDR: Code can be found here on Bitbucket.
The trick is to provide the uid and gid of the current user to a Dockerfile that modifies the default jenkins user.
The Dockerfile is pretty straightforward: