mavendockercontinuous-integrationgitlabcontinuous-delivery

Run (Docker) Test Container in gitlab with Maven


I am working in a gitlab ci/cd pipeline. This pipeline executes all of its commands (excluding the deployments) with maven and docker. In this case, I am trying to run integration tests (that are kicked off by maven), which use a test container (for the mysql database). These tests work fine when running locally. However, I am running into issues when I try to run them from gitlab. I am fairly certain my issue is I don't have access to docker (however, in this case, I need my image to have both docker and maven).

Here is the applicable section of gitlab file:

#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

Here is the error stack trace on 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

Before anyone suggests it, this testing works locally (and has worked locally for a long time). Here is part of the sample output:

[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

How should i fix this? Should i create my own base image which has maven and docker installed? Can I easily modify my container to use apt get in my container? Any suggestions are welcome, but I would like to have both docker and maven, since they are in my technology stack and working--just a gitlab config issue. Here is the project location on gitlab: https://gitlab.com/connorbutch/reading-comprehension-api

While i certainly welcome comments on other parts of the project, those should be directed to me via email,in order to keep this page clean (don't put them here)


Solution

  • For anyone who's wondering: You can fix this by adding a few variables to your CI/CD configuration.

    The ones I added were:

    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"