Facing issue in Jenkinfile while running the Jenkins Docker build agent

Hi,

I have a customized Docker image that I’m trying to run as a Jenkins docker build agent. Refer to the Jenkins official doc here Using Docker with Pipeline

However, getting this following error:-

**java.io.IOException: Failed to run image 'jenkinsagent/test-image:1.1.0'. Error: docker: invalid reference format.**

That argument ‘abc’ is not getting passed properly in the following piece of code of Jenkinsfile:-

 agent {
        docker {
          image 'jenkinsagent/test-image:1.1.0'
          args '-v ${HOME}/new/test/.aws:/root/.aws:ro -e XDG_CACHE_HOME=/tmp/go/.cache abc'
        }
      }

If I run the same container on my local terminal by sending the same argument ‘abc’ then it works fine:-

  `docker run -v $PWD/.aws:/root/.aws:ro -e XDG_CACHE_HOME=/tmp/go/.cache jenkinsagent/test-image:1.1.0 abc`

If I do not pass that argument then the container works fine in the Jenkins server as well however my requirement is to pass that argument only. May I know how can I pass any arguments in the following syntax of Jenkins docker build agent:-

    agent {
        docker {
          image 'jenkinsagent/test-image:1.0.0'
          args '-v ${HOME}/new/test/.aws:/root/.aws:ro -e XDG_CACHE_HOME=/tmp/go/.cache abc'
        }
      }

I think its your args abc, the docker image needs to go second last, then the command you want to run. docker.args will be prepended to the image, so it think the image is abc

I Would recommend letting jenkins handle your args for you, by adding environment { XDG_CACHE_HOME="/tmp/go/.cache" } instead of of doing it with -e
if your just wanting to run abc, you should sh("abc") inside of a step. Jenkins needs to run the container, then connect inside of it, usually uses cat to do this, then you can run normally.

So something like (untested and written by hand):

pipeline {
   agent {
     docker {
       image 'jenkinsagent/test-image:1.0.0'
       args '-v "${HOME}/new/test/.aws:/root/.aws:ro"'
    }
  }
environment {
  XDG_CACHE_HOME="/tmp/go/.cache"
  // I would also do HOME="${WORKSPACE} so you don't need the args -v
 }
stages {
  stage('Run abc') {
    steps {
      sh('abc')
     }
  }
}
}

or something. https://github.com/halkeye/infinicatr/blob/376aec6e28000e244f5d02f4155d405486f2d402/Jenkinsfile would be one example I have

1 Like