简体   繁体   English

使用 TestContainers 和 Jenkins 在 docker 容器中运行测试(docker.sock 权限被拒绝)

[英]Run tests in docker container using TestContainers and Jenkins (docker.sock Permission denied)

In my tests I use TestContainers .在我的测试中,我使用TestContainers I want run tests in container using Jenkins.我想使用 Jenkins 在容器中运行测试。 I created this image for run tests with maven .我为使用maven运行测试创建了这个图像。 Dockerfile: 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.在我的测试中,我使用MySQL Cluster容器。

For Jenkins I add node in Jenkinsfile:对于Jenkins我在 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.问题是 Jenkins 是从用户 jenkins 运行的。 I created the same user with the same userid and groupid.我使用相同的用户 ID 和组 ID 创建了相同的用户。 But when I start the tests I get that access to docker.sock is denied:但是当我开始测试时,我发现对docker.sock访问被拒绝:

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.我研究了这个页面: https : //www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/但它没有帮助我。 There everything starts from the root user, and of course he has no problems.一切都从root用户开始,当然他没有问题。

How can I fix it?我该如何解决?

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.这可能有一些安全风险,但我在我的本地机器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM