9
votes

what is the syntax for scripted pipeline(i.e. node{} block as the top-level) to use a docker container(from a dockerhub image or dockerfile for example)?

I know how to use declaritive pipeline to do this, just specify a agent block and put docker inside. I want to know how to use scripted pipeline syntax to do so.

1
You mean how to use DinD, DooD in Docker agents? jenkins.io/doc/book/pipeline/docker Simply click "Toggle Scripted Pipeline" under each example to compare with scripted version.Beri

1 Answers

0
votes

You can do this like:

node('docker-host') {
    checkout scm
    docker.withRegistry('registry-url', 'credentials-id') {
        def dockerfile = "path/to/Dockerfile"
        def buildImage = docker.build("my-image-${env.GIT_COMMIT}", "-f $dockerfile .")
        buildImage.inside('-v /tmp:/tmp') {
            echo "inside docker"
        }
    }
}