在Kubernetes环境Jenkins中运行KafkaContainer(Testcontainers)报错求助
问题描述
我有一个Java应用,测试代码使用Testcontainers启动Kafka容器:
kafka = new KafkaContainer( DockerImageName.parse("harbor-main.test.com/devbird/confluentinc/cp-kafka:7.3.2") .asCompatibleSubstituteFor("confluentinc/cp-kafka") ); kafka.withEmbeddedZookeeper().start(); ...
通过Kubernetes环境中的Jenkins,使用Maven容器执行mvn clean install构建。Jenkins Agent的Pod配置如下:
apiVersion: v1 kind: Pod metadata: namespace: project-jenkins spec: containers: - name: maven image: harbor-main.test.com/maven-jdk17-git:latest command: - cat tty: true volumeMounts: - name: maven-cache mountPath: /root/.m2/repository - name: dind-daemon image: harbor-main.test.com/docker:dind-rootless securityContext: privileged: true readOnlyRootFilesystem: false command: - cat tty: true volumeMounts: - name: docker-socket mountPath: /run/user/1001/docker.sock volumes: - name: docker-socket emptyDir: {} - name: maven-cache persistentVolumeClaim: claimName: maven-cache
执行构建时出现Testcontainers无法找到Docker环境的错误:
08:12:51 23/03/16 08:12:51 WARN TestcontainersConfiguration: Attempted to read Testcontainers configuration file at file:/root/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: /root/.testcontainers.properties (No such file or directory) 08:12:52 23/03/16 08:12:51 ERROR DockerClientProviderStrategy: Could not find a valid Docker environment. Please check configuration. Attempted configurations were: 08:12:52 UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)As no valid configuration was found, execution cannot continue. 08:12:52 Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.228 sec <<< FAILURE!
解决方案
核心问题分析
- Maven容器未挂载Docker Socket:Testcontainers默认查找
/var/run/docker.sock,但当前配置中仅dind-daemon容器挂载了socket,maven容器无法访问。 - Dind配置冲突:rootless模式的dind不需要
privileged: true,反而会引发权限问题;且dind-daemon使用cat命令未真正启动Docker守护进程。 - 缺少Testcontainers环境变量:未告知Testcontainers使用dind-daemon提供的socket路径。
具体修复步骤
1. 修正Jenkins Agent Pod配置
apiVersion: v1 kind: Pod metadata: namespace: project-jenkins spec: containers: - name: maven image: harbor-main.test.com/maven-jdk17-git:latest command: ["cat"] tty: true volumeMounts: - name: maven-cache mountPath: /root/.m2/repository # 共享docker socket给maven容器 - name: docker-socket mountPath: /run/user/1001/docker.sock # 配置环境变量指定Docker地址 env: - name: DOCKER_HOST value: unix:///run/user/1001/docker.sock - name: dind-daemon image: harbor-main.test.com/docker:dind-rootless securityContext: # rootless dind不需要特权模式 privileged: false readOnlyRootFilesystem: false runAsUser: 1001 # 启动dockerd而非cat command: ["dockerd", "--host=unix:///run/user/1001/docker.sock"] env: # 禁用TLS证书生成,避免连接问题 - name: DOCKER_TLS_CERTDIR value: "" volumeMounts: - name: docker-socket mountPath: /run/user/1001/docker.sock volumes: - name: docker-socket emptyDir: {} - name: maven-cache persistentVolumeClaim: claimName: maven-cache
2. 调整Jenkins构建命令
在Maven构建前确保环境变量生效(若Pod中已配置env,可直接执行):
stage('Build & Test') { steps { container('maven') { sh 'mvn clean install' } } }
3. 可选:添加Testcontainers配置文件
若需持久化配置,可在maven容器中创建/root/.testcontainers.properties:
docker.host=unix:///run/user/1001/docker.sock
内容的提问来源于stack exchange,提问作者leotim
相关产品推荐
相关产品推荐

