Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running commands inside docker container created using Dockerfile in Jenkinsfile

I have created a Jenkins pipeline file and created a Jenkinsfile. Inside Jenkinsfile I have used Dockerfile agent and then running some steps. The steps are supposed to run inside docker but they are running on the host. Hers the code.

#!/usr/bin/env groovy
@Library(['abc-jenkins','xyz-jenkins-library']) _
pipeline {
agent {
    dockerfile{
        dir 'TEST'
        filename 'dockerfile'
        label 'docker'
    }
}
stages {
    stage('Build Stage') {
        steps {
            echo 'testing stage running'
            sh "ls"
        }
    }
}   
}
like image 790
sanjay patel Avatar asked Oct 18 '25 08:10

sanjay patel


2 Answers

You misunderstand it according to the output of ls.

For Jenkins pipeline docker DSL, it will mount the jenkins job workspace to docker container and change the docker WORKDIR to the jenkins job workspace.

Therefor when you execute ls inside container, it will print the files & fodler under your jenkins job workspace.

You can try other cmd or ls other folder that not exists in host to approve it's really be executed inside container rather than host.

like image 197
yong Avatar answered Oct 21 '25 01:10

yong


You may using -> docker.image('image').inside{..}

stage('BuildInside') {
         docker.image('ubuntu1804').withRun('-d=true -p 8888:8080') {c ->
            docker.image('ubuntu1804').inside{
               /*  Do something here inside container  */
               sh "ls"
            }
        }
  }
like image 37
Michael Avatar answered Oct 21 '25 03:10

Michael