首页 > 解决方案 > Testcontainers 无法在 Docker 中获取 Jenkins kubernates docker 上的映射端口

问题描述

我正在尝试在 Docker 容器中的 Jenkins Kubernetes Docker 上使用 Testcontainers 运行我的集成测试。

测试容器版本:1.15.3

但是,它总是无法在 DinD 容器中获取 Container.getMappedPort(X)。

它在我的本地设置上工作得非常好,并设法获取映射的端口。

有没有人遇到过这个问题或有解决方案?

我的詹金斯文件

#!groovy

def label = "debug-${UUID.randomUUID().toString()}"

podTemplate(label: label, slaveConnectTimeout: '10', containers: [
        containerTemplate(
                name: 'docker-in-docker',
                image: 'cfzen/dind:java11',
                privileged: true,
                workingDir: '/home/jenkins/agent',
                ttyEnabled: true,
                command: 'cat',
          envVars: [
            envVar(key: 'TESTCONTAINERS_HOST_OVERRIDE', value: 'tcp://localhost:2375'),
            envVar(key: 'TESTCONTAINERS_RYUK_DISABLED', value: 'true'),
          ]
        ),
        containerTemplate(
                name: 'helm-kubectl',
                image: 'dtzar/helm-kubectl',
                workingDir: '/home/jenkins/agent/',
                ttyEnabled: true,
                command: 'cat'
        )
],
        volumes: [hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),],
        annotations: [
                podAnnotation(key: 'iam.amazonaws.com/role',
                        value: 'arn:aws:iam::xxxxxxxxxxx')
        ],
)
        {
            node(label) {

                deleteDir()

                stage('Checkout') {
                    checkout scm
                    def shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
                    currentBuild.description = "${shortCommit}"
                }


              stage('Run Integration tests') {
                container('docker-in-docker') {
                  withCredentials([
                    usernamePassword(credentialsId: 'jenkins-artifactory-credentials',
                      passwordVariable: 'ARTIFACTORY_SERVER_PASSWORD',
                      usernameVariable: 'ARTIFACTORY_SERVER_USERNAME')])
                    {
                  echo 'Run Integration tests'
                  sh("mvn -B clean verify -q -s  mvn/local-settings.xml")
                }
            }
        }

测试运行者:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "classpath:features")
public final class RunCucumberIT {


    @BeforeClass
    public static void init(){
        Containers.POSTGRES.start();
        System.out.println("Exposed port of db is"+Containers.POSTGRES.getExposedPorts());
        System.out.println("Assigned port of db is"+Containers.POSTGRES.getFirstMappedPort());
        Containers.WIREMOCK.start();
        Containers.S3.start();
    }

    private RunCucumberIT() {

    }

}

在 Containers.POSTGRES.getFirstMappedPort() 失败

Requested port (X) is not mapped

标签: dockerjenkins-pipelinespring-testtestcontainers

解决方案


推荐阅读