0

I'm currently trying to deploy a Golang API using Jenkins on Docker. I've already configured everything so that Jenkins would have access to the correct repository it supposed to access. A Jenkinsfile is provided so that it would use the command docker-compose to set up the build. So this means that the API itself will be containerized.

The Jenkinsfile looks something like this.

pipeline {
    agent any 
    stages {
        stage('deploy') {
            steps {
                dir("swagger/"){
                    sh """
                    sed -i 's/\\/api\\/v1/\\/dev\\/api\\/v1/g' swagger.json && sed -i 's/\\/uploads/\\/dev\\/uploads/g' swagger.json
                    """
                }
                dir("deployments/docker/") {
                    sh "export JENKINS_NODE_COOKIE=dontKillMe && ./start.sh"
                }
            }
        }
    }
    post {
        success { 
            emailext body: 'Good news! The build bare fruits!', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Build Successful'
         }
        failure {
            emailext body: 'Try again probs?', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Build Failed'
        }
    }
}

The deployment part starts when Jenkins tries to run ./start.sh. The file start.sh has only one line that to run the command docker-compose --compatibility up -d --build.

However, as I soon found out, when Jenkins was trying to build the API, it performs the build inside of the Docker container instead of on the host machine. I want to make sure the API is built on the host machine and not on the container.

In other words, this means that I will be having a docker container within a docker container. I'm not really sure, but that's not what I want specifically. What I want is to make sure that both the Jenkins and API docker containers are running in the same host machine.

I've read a couple of suggestions from here and here, but it seems I didn't found the answer I needed.

My question would be How to make Jenkins on Docker perform the build on the host machine instead of the container? Is there something that I might be missing? Thanks in advance

Imperator123
  • 599
  • 2
  • 11
  • 25
  • Can you include your `Jenkinsfile` or similar setup? Jenkins knows how to bind-mount the workspace directory into a build container, so it's actually reasonable to do the build "in a container" to get back an artifact that you can publish to somewhere else. – David Maze Feb 12 '21 at 14:16
  • @DavidMaze I did some edits, so please check out what the file looks like. I'm not sure I'm following. So do you mean that we can perform the build inside of the Jenkins docker container, but have the final container of the API in the host machine? – Imperator123 Feb 12 '21 at 14:42

2 Answers2

0

As I understood your question you need to configure host machine in jenkins as slave/agent. Jenkins master will be running in docker container on the host and host itself will be connected to the jenkins container as slave.

Dmitriy Tarasevich
  • 1,082
  • 5
  • 6
0

I was able to solve it. I haven't try @Dmitriy Tarasevich's solution, But definitely worth a try.

What I ended up doing is just is expose the Docker socket in the host machine to the docker container that contains Jenkins. Reference here.

Imperator123
  • 599
  • 2
  • 11
  • 25