My Jenkins Docker image contains Docker itself. The reason is to have access to Docker during deployment jobs. This is just a solution that works very fine for me.
After starting the container, I can execute Jenkins jobs, e.g. to maven-build and then docker-build, tag and push images. Works great! To execute the docker commands, I included access to Docker Engine from within the container. Via the docker.sock I can access the Docker engine. The container runs in an existing Docker environment. Notice: alternatively, I could have added the Docker tooling later via the Jenkins GUI. This option runs for over a year well.
The problem I have is that the Docker engine cannot be accessed. The reason is that the /var/run/docker.sock has wrong permissions. So, after starting the Docker image (or container) I have to manually change (as a root user) the /var/run/docker permissions to 777.
How can I change the image to automatically have the right execution permissions for the /var/run/docker.sock? The container runs as a jenkins user.
This is my container:
FROM jenkins/jenkins:lts
USER root
RUN mkdir -p /tmp/download && \
curl -L https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz | tar -xz -C /tmp/download && \
rm -rf /tmp/download/docker/dockerd && \
mv /tmp/download/docker/docker* /usr/local/bin/ && \
rm -rf /tmp/download && \
curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
groupadd -g 998 docker && \
usermod -aG staff,docker jenkins
...
user jenkins