Hammed
Hammed

Reputation: 1557

Unable to build docker image in jenkins with additional flags

I'm building and pushing a docker image to ecr as part of a jenkins pipeline.

    environment {
        registry = "11111111111.dkr.ecr.eu-west-2.amazonaws.com/bar"
        registryCredential = 'ecr-creds'

    stages {
        stage('Docker Build') {
            steps {
                dir('assets/'){
                script {
                  dockerImage = docker.build registry
            }
                }
            }
        }
        stage('Docker Deploy') {
            steps{
                script {
                docker.withRegistry("https://" + registry, "ecr:eu-west-2:" + registryCredential ) {
                  dockerImage.push()
                    }
                }
            }
        }

However, I need to add the flag --network host as part of my docker build argument as its the only way I'm able to successfully build the image.

Doing this sh "docker build -t $registry . --network host" works, but then the Docker Deploy stage fails with a No such property: dockerImage error which is understandable as dockerImage isn't defined anymore.

Is there a way to add the --network host flag?

Upvotes: 0

Views: 451

Answers (1)

David Maze
David Maze

Reputation: 158696

Using Docker with Pipeline in the Jenkins documentation notes:

It is possible to pass other arguments to docker build by adding them to the second argument of the build() method. When passing arguments this way, the last value in the that string must be the path to the docker file and should end with the folder to use as the build context)

So, you should be able to write:

stage('Docker Build') {
  steps {
    dockerImage = docker.build(registry, '--network=host assets')
  }
}

(You shouldn't usually need host networking in Docker, especially during the image-build phase, but if you're working around a network-configuration issue it can be a little tricky to figure out.)

Upvotes: 2

Related Questions