9

In my tests I use TestContainers. I want run tests in container using Jenkins. I created this image for run tests with maven. Dockerfile:

FROM registry.company.com/maven:3-jdk-8-slim

RUN apt update
RUN apt install -y wget libatomic1 curl gnupg

RUN wget https://dev.mysql.com/get/Downloads/MySQL-Cluster-7.6/ndbclient_7.6.10-1debian9_amd64.deb \
&& dpkg -i ndbclient_7.6.10-1debian9_amd64.deb && rm ndbclient_7.6.10-1debian9_amd64.deb
RUN ln -s /usr/lib/x86_64-linux-gnu/libndbclient.so.6.1.0 /usr/lib/x86_64-linux-gnu/libndbclient.so

RUN curl -sL https://deb.nodesource.com/setup_10.x  | bash -
RUN apt-get -y install nodejs

RUN groupadd --gid 10000 ldap && useradd -m --uid 10028 --gid 10000 jenkins
RUN echo "jenkins ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers

COPY settings.xml /home/jenkins/

USER jenkins

In my tests I use MySQL Cluster container.

For Jenkins I add node in Jenkinsfile:

node('Docker') {
    checkout scm

    docker.withRegistry('http://registry.company.com/v2/', 'cred_id') {
        docker.image('integration-tests:latest')
                .inside('-v $HOME/.m2:/home/jenkins/.m2:rw -u jenkins') {
                    stage('Test') {
                        sh 'mvn clean -s /home/jenkins/settings.xml'
                        sh 'mvn -s /home/jenkins/settings.xml test -DargLine="-Djava.library.path=/usr/lib/x86_64-linux-gnu/"'
                    }
                }
    }
}

The problem is that Jenkins is running from the user jenkins. I created the same user with the same userid and groupid. But when I start the tests I get that access to docker.sock is denied:

Caused by: java.io.IOException: com.sun.jna.LastErrorException: [13] Permission denied
    at org.testcontainers.shaded.org.scalasbt.ipcsocket.UnixDomainSocket.<init>(UnixDomainSocket.java:62)
    at org.testcontainers.dockerclient.transport.okhttp.UnixSocketFactory$1.<init>(UnixSocketFactory.java:27)
    at org.testcontainers.dockerclient.transport.okhttp.UnixSocketFactory.createSocket(UnixSocketFactory.java:27)

I studied this page: https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/ but it didn't help me. There everything starts from the root user, and of course he has no problems.

How can I fix it?

All_Safe
  • 1,339
  • 2
  • 23
  • 43
  • 4
    Can you try adding `jenkins` user to `docker` group. Check this for more info https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user – mchawre Jun 19 '19 at 07:09

1 Answers1

11

A dirty fix is:

sudo chmod a+rw /var/run/docker.sock

This might have some security risks, but I am on my local machine.

Gismo Ranas
  • 6,043
  • 3
  • 27
  • 39