Building docker images with declarative pipeline

7,407 views
Skip to first unread message

Thomas Fjellstrom

unread,
Feb 14, 2017, 2:32:56 PM2/14/17
to Jenkins Users
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

Andrew Bayer

unread,
Feb 14, 2017, 4:35:19 PM2/14/17
to jenkins...@googlegroups.com
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Fjellstrom

unread,
Feb 14, 2017, 7:24:18 PM2/14/17
to Jenkins Users
That would explain the complete lack of examples. I'm kindof surprised there wasnt some kind of mapping from plugin dsl to the new declarative syntax, to at least allow some use of the existing apis. The script blocks are good enough for now however.

Thanks :)


On Tuesday, February 14, 2017 at 2:35:19 PM UTC-7, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.
On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <tfjel...@gmail.com> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Max Schindler

unread,
Apr 26, 2017, 6:40:09 AM4/26/17
to Jenkins Users
I would also be very interested in this. @Thomas: How are you proceeding with your topic? Maybe we can share our experience and help each other on this topic?

drew....@computecanada.ca

unread,
Jun 1, 2017, 2:23:11 AM6/1/17
to Jenkins Users
Hello Andrew and Thomas,

I ran in this exact problem and used a script{} block, but I am wondering if there are updates to this issue.  All of the documentation/examples I have seen use scripted syntax, but sometimes the documentation is behind the code. :)

Cheers,
Drew.


On Tuesday, February 14, 2017 at 1:35:19 PM UTC-8, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.
On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <tfjel...@gmail.com> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Alex B

unread,
Sep 11, 2017, 5:57:23 PM9/11/17
to Jenkins Users
Hi Drew,

Could you post where you found the example using script{} block? I'm looking into building Docker images in ephemeral jenkins agents but having difficulty finding proper documentation for the syntax.

-Alex

Joshua Noble

unread,
Sep 12, 2017, 12:42:03 PM9/12/17
to Jenkins Users
With declarative pipelines this is pretty straight forward, just build the Docker image like you would on your local machine using `sh` commands.

For example, in our cluster all build agents are actually the same, but most builds run inside of a container on one of these nodes, which you've noted (correctly) that you don't want to do, since you want to build an image. We've simply given the "docker" label to our build agents, and our declarative Jenkinsfile's use that label under the agent option.

For example, you could use the following Jenkinsfile to build your-app. Note that I'm using a GIT_SHA variable here which actually doesn't resolve, but you could easily write a pipeline shared library to provide this info. In addition, I only like to publish master branch builds on some repos in order to save space in our registry. You can even tell Jenkins to use specific credentials when pushing.

pipeline {
  agent { label 'docker' }
  options {
    ansiColor colorMapName: 'XTerm'
  }
  stages {
    stage('Build') {
      steps {
        sh "docker build -t registry.yourcompany.com/company/your-app:${GIT_SHA} ."
      }
    }
    stage('Publish') {
      when {
        branch 'master'
      }
      steps {
        withDockerRegistry([credentialsId: 'registry-creds', url: 'https://registry.yourcompany.com']) {
        }
      }
    }
  }
}


Ari Pringle

unread,
Sep 12, 2017, 3:09:17 PM9/12/17
to Jenkins Users
Hi Alex,

I had some trouble figuring out how to do this as well. The docs here show what is possible for docker.image using scripted pipeline (and thus inside of a script{} block): https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#docker-workflow

I ended up with something like this:

script {
    def image = docker.build("image-name:test", '-f Dockerfile.dev .')
    // can then call methods like image.push() here, or run a command with image.inside():
    image.inside('--net=host -v /mount:/mount:ro') {
       sh 'some-command.sh'
    }
}

- Ari

Mayur Nagekar

unread,
Jan 11, 2018, 9:44:48 AM1/11/18
to Jenkins Users
Hello all,

Do we have any update here ? i have been trying to use dockerfile as the agent to build a docker image from the Dockerfile in my source repo and then be able to run some unit tests within the container. 

I browsed all possible sources of documentation(see below) but still no luck. Has the cloudbees or jenkins guys been able to make progress here ? Or may be I don't know.

Appreciate your help here.

Thanks,
Mayur



On Wednesday, February 15, 2017 at 3:05:19 AM UTC+5:30, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.
On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <tfjel...@gmail.com> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Toby Hersey

unread,
Jun 4, 2019, 3:47:08 PM6/4/19
to Jenkins Users
Thanks very much for this :)
Reply all
Reply to author
Forward
0 new messages