Why is Jenkins not using the correct image for docker builds?

Viewed 232

No matter what I try, I seem to be unable toget a declerative pipeline to build my project inside a docker container, with the correct image.

I have verified the following:

  • Jenkins does build the correct image (based on messages in the log)
  • When I build the image manually, it is build correctly
  • When building the project inside a container with the correct image, the build succeeds
  • The Jenkins steps do run in a container with some image.

As far as I can tell, Jenkins simply uses the base image and not the correct one, resulting from the dockerfile I specify.

Things I've tried:

Let Jenkins figure it out

pipeline {
    agent dockerfile

Using docker at the top level:

pipeline {
    agent {
        dockerfile { 
            filename 'Dockerfile'
            reuseNode true
        }
    }
    stages {
        stage('configure') {
            steps {

Use docker in each step

pipeline {
    agent none
    stages {
        stage('configure') {
            agent {
                dockerfile { 
                    filename 'Dockerfile'
                    reuseNode true
                }
            }
            steps {

Abbreviations, due to the number of examples. Docker is not mentioned anywhere outside of the specified areas and simply removing the docker parts and using a regular agent works fine.

Logs

The logs are useless. They simply state that they build the image and verify that they exist and then fail to execute commands that have just been installed (meson in this case).

2 Answers

First of all, I suggest you to read:

Without any logs or a more detailed explanation on your setup, it is difficult to help you. I can certainly tell you that the second try is wrong because

reuseNode is valid for docker and dockerfile, and only has an effect when used on an agent for an individual stage.

Instead, I am not sure how the third try could ever work: with agent none you are forcing each stage to have an agent section, but in the stage's agent section you have the reuseNode option set to true. Isn't it a contradiction? How could you reuse a top-level node if this one does not exist?

I know it is not an answer, but it is also too long to stay in the comments in my opinion.

I always use it like this, with a pre-build image:

pipeline {
  agent {
    docker { image 'node:16-alpine' }
  }
  stages {
    stage('Test') {
      steps {
         sh 'node --version'
      }
    }
  }
}

But I can only guess what you want to do inside the docker environment.

Related