skip to Main Content

There is a docker image whose name is IMAGE_NAME.

I want a Jenkins job to docker pull IMAGE_NAME and then docker run IMAGE_NAME /bin/bash -c '/usr/bin/my_startup arg1 arg2 arg3

I tried a few variations of a Jenkinsfile based on some googled examples but they don’t work and I clearly don’t understand stages, agent, what environment the steps {sh '...'} command runs in, or any of the Jenkins jargon at all.

What’s a minimal jenkinsfile which will pull IMAGE_NAME and run a command with arguments in it?

2

Answers


  1. Chosen as BEST ANSWER

    This worked:

    docker.image('MY_IMAGE').inside {
      sh '/bin/my-command my-args'
    }
    

    Apparently this is a Pipeline script, run in a Groovy sandbox in Jenkins 2.150.1.


  2. minimal jenkinsfile

    As long as this is not a "code golf" style question, the following pipeline will do the trick for you:

    pipeline {
      agent any // modify as needed for an agent that supports container runtime
    
      stages {
        stage('Pull Image and Run Container') {
          script {
            // pulls image and then executes as container; note bonus example of arguments to container instantiation if needed
            docker.image('IMAGE_NAME').withRun('-e "env_var=env_value" -p 1234:5678') { container -> // container is lambda scope variable which you can reference as needed
              // execute startup
              sh(label: 'Startup', script: '/usr/bin/my_startup arg1 arg2 arg3')
            }
          }
        }
      }
    }
    

    If you have a private and/or custom registry, then you will need the withRegistry block and a registry credentials plugin setup on the Jenkins main server.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search