Pinki Sharma
Pinki Sharma

Reputation: 163

Testcontainers with Jenkins CI/CD : Couldn't find a valid docker environment

I am using testcontainers for the integration test with Oracle DB in the Spring boot application. I installed Docker Desktop to make it work in my local environment (Windows) but whenI run the Jenkins pipeline then tests are failing with error "Couldn't find valid docker environment"

Jenkins uses available nodes(jnlp) for the build as we are using docker to create the application image for the deployment then why build step (tests) is failing with this error.

Please guide what i need to do to make it pass in the pipeline.

enter image description here

DemoClass1Test> initializationError FAILED
    java.lang.IllegalStateException at DockerClientProviderStrategy.java:232
DemoClass2Test> initializationError FAILED
    java.lang.IllegalStateException at DockerClientProviderStrategy.java:232


java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
    at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$7(DockerClientProviderStrategy.java:277)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:268)

pipeline - env.gradleTasks = gradle clean build

pipeline {
    agent {
        kubernetes {
            inheritFrom used_agent
            defaultContainer 'builder-alpine'
        }
    }

    stages {
        stage ('Welcome to our Pipelines!') {
            steps {
                script {
                    log.banner()
                }
            }
        }
        stage ('Source Code: Clone') {
            when {
                expression { env.REL_URL != null && env.REL_URL != ""}
            }
            steps {
                container('jnlp') {
                    script {
                        util.gitClone(REL_URL, branch)
                    }
                }
            }
        }
        stage ('Gradle Build') {
            when {
                expression { build.toLowerCase() == "gradle" && env.REL_URL != null && env.REL_URL != "" }
            }
            steps {
                script {
                    buildTools.gradle(env.gradleTasks)
                }
            }
        }
}

Upvotes: 0

Views: 1860

Answers (1)

gmode
gmode

Reputation: 3740

Testcontainer spins up docker containers, that means whatever image Jenkins agent uses, it must support running docker inside it. You can read more here and example here how to get docker inside docker running for CI. In a nutshell:

Give the wrapping container of the Jenkins Agent access to the host network by having Jenkins provide a --network="host" option to its docker run command:

agent {
    dockerfile {
        filename 'Dockerfile.jenkinsAgent'
        additionalBuildArgs  ...
        args '-v /var/run/docker.sock:/var/run/docker.sock ... --network="host" -u jenkins:docker'
   }
}

To do via kubernetes agent, you can use this example:

agent {
    kubernetes {
      yaml '''
        apiVersion: v1
        kind: Pod
        spec:
          containers:
          - name: maven
            image: maven:alpine
            command:
            - cat
            tty: true
          - name: docker
            image: docker:latest
            command:
            - cat
            tty: true
            volumeMounts:
             - mountPath: /var/run/docker.sock
               name: docker-sock
          volumes:
          - name: docker-sock
            hostPath:
              path: /var/run/docker.sock    
        '''
    }

Upvotes: 1

Related Questions