使用 Maven 在 gitlab 中运行(Docker)测试容器

问题描述 投票:0回答:2

我正在 gitlab ci/cd 管道中工作。该管道使用 maven 和 docker 执行其所有命令(不包括部署)。在本例中,我尝试运行集成测试(由 Maven 启动),它使用测试容器(用于 mysql 数据库)。这些测试在本地运行时工作正常。然而,当我尝试从 gitlab 运行它们时遇到了问题。我相当确定我的问题是我无法访问 docker(但是,在这种情况下,我需要我的映像同时具有 docker 和 maven)。

这是 gitlab 文件的适用部分:

#This phase is only run when merging (to master) 
merge_tests:
 image: maven:latest
 stage: mvn_build_and_test
 #TODO can remove services if this does not work    without docker image as base image
  services:
  - docker:stable-dind
 #set variables for use later when running maven in script section
variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"  
script: 
  #ONLY RUN WHEN MERGE REQUEST IS TO MASTER BRANCH
  #if ["$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" == "master"]; then 
  #check docker is installed and running
  #- docker --version #this fails if uncommented
  - mvn $MAVEN_CLI_OPTS clean install -Pintegration-test-profile
  #; fi   
#only:
 #- merge_requests

这是 gitlab 上的错误堆栈跟踪:

 org.testcontainers.containers.ContainerLaunchException: Container startup failed
1958 Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageNameFuture=java.util.concurrent.CompletableFuture@21baa903[Completed normally], imagePullPolicy=DefaultPullPolicy(), dockerClient=LazyDockerClient.INSTANCE)
1959 Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

在有人建议之前,这个测试在本地有效(并且已经在本地工作了很长时间)。这是示例输出的一部分:

[INFO] --- maven-failsafe-plugin:2.22.1:integration-test (integration-tests) @ reading-comprehension-api ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.connor.retrieving.GetByIsbnIT
-----content ommitted for brevity----------------------------    
Tests run: 3, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 10.451 s - in com.connor.adding.AddAssessmentIT      
--content ommitted, but test containers start  
[INFO] --- maven-failsafe-plugin:2.22.1:verify (integration-tests) @ reading-comprehension-api ---
[INFO] 
[INFO] --- maven-cucumber-reporting:2.8.0:generate (addCucumberReport) @ reading-comprehension-api ---
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
[INFO] About to generate Cucumber report.
[INFO] 
[INFO] --- maven-cucumber-reporting:2.8.0:generate (getCucumberReport) @ reading-comprehension-api ---
[INFO] About to generate Cucumber report.
[INFO] 
[INFO] --- maven-cucumber-reporting:2.8.0:generate (updateCucumberReport) @ reading-comprehension-api ---
[INFO] About to generate Cucumber report.
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ reading-comprehension-api ---
[INFO] Installing /home/connor/Desktop/code/reading-comprehension-api/target/reading-comprehension-api-0.0.1-SNAPSHOT.jar to /home/connor/.m2/repository/com/connor/reading-comprehension-api/0.0.1-SNAPSHOT/reading-comprehension-api-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/connor/Desktop/code/reading-comprehension-api/pom.xml to /home/connor/.m2/repository/com/connor/reading-comprehension-api/0.0.1-SNAPSHOT/reading-comprehension-api-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  38.282 s
[INFO] Finished at: 2019-12-04T22:31:23-05:00

我应该如何解决这个问题?我应该创建自己的安装了 Maven 和 docker 的基础镜像吗?我可以轻松修改我的容器以在我的容器中使用 apt get 吗?欢迎任何建议,但我想要 docker 和 maven,因为它们在我的技术堆栈中并且正在工作——只是 gitlab 配置问题。 这是 gitlab 上的项目位置:https://gitlab.com/connorbutch/reading-compressive-api

虽然我当然欢迎对项目其他部分的评论,但这些评论应该通过电子邮件发送给我,以保持此页面干净(不要将它们放在这里)

maven docker continuous-integration gitlab continuous-delivery
2个回答
2
投票

对于任何想知道的人:您可以通过在 CI/CD 配置中添加一些变量来解决此问题。

我添加的有:

MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"  
#these were added to try and get test containers to work -- if it doesnt, can remove below two
DOCKER_HOST: "tcp://docker:2375"
DOCKER_DRIVER: "overlay2" 

0
投票

对于任何运行 kubernetes runner 的人,必须将 runner 配置为“特权”才能运行 dind 服务:

  runners:
  ## Run all containers with the privileged flag enabled
  ## This will allow the docker:stable-dind image to run if you need to run Docker
  ## commands. Please read the docs before turning this on:
  ## ref: https://docs.gitlab.com/runner/executors/kubernetes.html#using-docker-dind
  ##
  privileged: true

https://docs.gitlab.com/runner/install/kubernetes.html#running-privileged-containers-for-the-runners

values.yaml 中的示例

© www.soinside.com 2019 - 2024. All rights reserved.