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

Run (Docker) Test Container in gitlab with Maven

我在 gitlab ci/cd 管道中工作。此管道使用 maven 和 docker 执行其所有命令(不包括部署)。在这种情况下,我正在尝试 运行 集成测试(由 maven 启动),它使用测试容器(用于 mysql 数据库)。在本地 运行ning 时,这些测试工作正常。但是,当我尝试从 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-comprehension-api

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

对于任何想知道的人:您可以通过在 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" 

对于 运行ning kubernetes 运行ners 的任何人,运行ners 必须配置为 运行 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