Testcontainers in Gitlab

4.7k Views Asked by At

I'd like to run Testontainers in Gitlab for testing Spring Boot applicaton. After I created Gitlab runner (changed URL and token):

sudo gitlab-runner register -n \
  --url https://gitlab.com/ \
  --registration-token REGISTRATION_TOKEN \
  --executor docker \
  --description "My Docker Runner" \
  --docker-image "docker:19.03.12" \
  --docker-privileged \
  --docker-volumes "/certs/client"

I've written a simple ci pipeline:

stages:
  - test

services:
  - docker:19.03.12-dind

variables:
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
    
test:
  stage: test
  image: amazoncorretto:11
  script:
    - java -version
    - chmod +x gradlew
    - ./gradlew test
  tags:
    - docker-dind

The test looks like:

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringBootTest
@Testcontainers
class DemoApplicationTests {
    
    @Container
    protected static GenericContainer<?> kc = new GenericContainer<>("jboss/keycloak")
            .withEnv("KEYCLOAK_USER", "admin")
            .withEnv("KEYCLOAK_PASSWORD", "admin")
            .withExposedPorts(8080)
            .waitingFor(Wait.forHttp("/auth").forStatusCode(200));

    @Test
    void contextLoads() {
    }
}

Instead of Keycloak it could be something else, but the result would be the same:

DemoApplicationTests > initializationError FAILED
    org.testcontainers.containers.ContainerLaunchException at GenericContainer.java:327
        Caused by: org.testcontainers.containers.ContainerFetchException at GenericContainer.java:1278
            Caused by: java.lang.IllegalStateException at DockerClientProviderStrategy.java:214s

It looks to me, that build container (amazoncorreto:11) is not run as a docker in docker. If I check DockerClientProviderStrategy.java:214, I can see:

Could not find a valid Docker environment. Please see logs and check configuration

The Testontaniners version is 1.15.0. What I'm doing wrong? Am I mission something?

1

There are 1 best solutions below

0
On

I'm using Spring Boot with TestContainers and gitlab runner. Full solution I wrote here https://stackoverflow.com/a/77888610/9209083
But my short answer is you need to use image which contains both maven and dind
For ex:

maven-job:
  image: intension/docker-dind-maven:3.8.5-r0-openjdk17
  stage: build

  variables:
    DOCKER_TLS_CERTDIR: ""

  script:
    - mvn --version  # check that mvn is working
    - docker images  # check that docker is working
    - mvn $MAVEN_CLI_OPTS clean package