Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: Connect to a Docker container from a stage that is run with an agent (another Docker container)

I am in the process of reworking a pipeline to use Declarative Pipelines approach so that I will be able to use Docker images on each stage.

At the moment I have the following working code which performs integration tests connecting to a DB which is run in a Docker container.

node {
    // checkout, build, test stages...
    stage('Integration Tests') {            
        docker.image('mongo:3.4').withRun(' -p 27017:27017') { c ->
        sh "./gradlew integrationTest"
    }
}

Now with Declarative Pipelines the same code would look somehow like this:

pipeline {
    agent none
    stages {
        // checkout, build, test stages...
        stage('Integration Test') {
            agent { docker { image 'openjdk:11.0.4-jdk-stretch' } }
            steps {
                script {
                    docker.image('mongo:3.4').withRun(' -p 27017:27017') { c ->
                        sh "./gradlew integrationTest"
                    }
                }
            }
        }
    }
}

Problem: The stage is now run inside a Docker container and running docker.image() leads to docker: not found error in the stage (it is looking for docker inside the openjdk image which is now used).

Question: How to start a DB container and connect to it from a stage in Declarative Pipelines?

like image 644
Sasha Shpota Avatar asked Sep 04 '19 09:09

Sasha Shpota


People also ask

Which network will the container be attached to when running it with Docker run?

This adds the busybox container to the my-net network. You can also choose the IP addresses for the container with --ip and --ip6 flags when you start the container on a user-defined network. If you want to add a running container to a network use the docker network connect subcommand.

How does Jenkins Pipeline define agent?

The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

Which command should be used to start Docker service in the Jenkins Docker container?

In the command box enter "sudo docker run hello-world" Click "Save". Click "Build Now".


1 Answers

What essentially you are trying is to use is DIND.

You are using a jenkins slave that is essentially created using docker agent { docker { image 'openjdk:11.0.4-jdk-stretch' } }

Once the container is running you are trying to execute a docker command. the error docker: not found is valid as there is no docker cli installed. You need to update the dockerfile/create a custom image having openjdk:11.0.4-jdk-stretch and docker dameon installed.

Once the daemon is installed you need to volume mount the /var/run/docker.sock so that the daemon will talk to the host docker daemon via socket.

The user should be root or a privileged user to avoid permission denied issue.

like image 136
error404 Avatar answered Sep 16 '22 19:09

error404