Jenkins always pull docker image from a declarative pipeline agent definition

Viewed 10242

I have a working Jenkins pipeline defined with agent {any}.

But I want to try running the tests on a docker image. According to documentation I can do that the following way:

pipeline {
     agent {
         docker { image 'centos/python-27-centos7:latest' }
     }
     stages {
        ...
     }
}

The problem is that my instance of Jenkins is offline and with this configuration it will try to docker pull the image when running the project.

However I have the centos/python-27 image loaded in the docker (visible with docker images and it can be run with docker run -i -t centos/python-27-centos7 /bin/bash

Is there any way I can configure the agent in Jenkins so it doesn't try to pull the image from Docker Hub?

3 Answers

The Docker agent has an alwaysPull flag you can set to false or true:

agent {
  docker {
    alwaysPull false
    image 'foo/bar'
  }
}

Jenkins has a Pipeline Syntax Generator that can help in these cases. In the Job menu on the left side, there is an icon for "Pipeline Syntax".

enter image description here

There you can see some settings to help you generate your pipeline directives.

enter image description here

You could try to use a dockerfile agent. If everything you need to build the image is local, then you won't have to be online.

pipeline {
 agent {
   dockerfile {
     filename "someOtherDockerfile"
     label "docker-nodes"
     args "-v /tmp:/tmp"
   }
 }
 stages {
    ...
 }

Your other option is possibly setting up a private docker registry. https://docs.docker.com/registry/deploying/

You should us siblings instead of nested container images. i.e. Try to mount the volume of docker daemon sockets to the Jenkins container via -v /var/run/docker.sock:/var/run/docker.sock. This way, you will get access to the images available on your host system.

Related