This is my dockerfile i'm using for the container/agent
FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
RUN groupadd -g 999 docker
# Install Docker CLI
RUN apt-get update && apt-get install -y docker.io
# Install OpenJDK, Git, and other build tools
RUN apt-get install -y openjdk-17-jdk git wget
RUN wget https://mirrors.estointernet.in/apache/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
RUN tar -xvf apache-maven-3.8.5-bin.tar.gz
RUN mv apache-maven-3.8.5 /opt/
ENV M2_HOME='/opt/apache-maven-3.8.5'
ENV PATH="$M2_HOME/bin:$PATH"
# Set up your user and workspace
RUN useradd -m -u 1005 -g 999 -s /bin/bash jenkins
USER jenkins
WORKDIR /home/jenkins/agent
My pipeline where 1005 is my jenkins user and 999 is my docker group
pipeline {
agent { docker {
image 'my docker img'
args '-u 1005:999 -v /var/run/docker.sock:/var/run/docker.sock --entrypoint=""'
registryCredentialsId 'hubdocker'
alwaysPull true
} }
stages {
stage('Print User and Groups') {
steps {
script {
// Print current user
sh 'id'
}
}
}
...
stage('docker build') {
steps {
sh 'docker build -t myregistrery/test1 .'
}
}
}
The result for id command logged as 1005 on the host and in the container is the same. (exept for roles that are not needed in the container)
(result) uid=1005(jenkins) gid=999(docker) groups=999(docker)
I have notice that when I use ls -ln /var/run/docker.sock inside the container it says srw-rw---- 1 65534 65534
I think that's the problem, it there a way to map the mounted docker.sock to keep host permission or a work around?
I don't want to have to change the perm of docker.sock in the host.
Note that I can reproduce the problem by running the docker agent in the terminal without using jenkins.
I am getting permission denied when a docker command is used in the container
for exemple 'docker ps' will throw that error. Same thing for sh 'docker build -t myregistrery/test1 .' in the pipeline.
docker build -t myregistrery/test1 . Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=jplamondon%2Ftest1&target=&ulimits=null&version=1": dial unix /var/run/docker.sock: connect: permission denied