cdm
cdm

Reputation: 799

Run Jenkins Pipeline Tests Inside Docker Container

I'm trying to run my pipeline tests inside (or against) a docker container that I have. I originally used a Dockerfile to create the container, but the tests would always run against the jenkins node executing the job. I modified my jenkinsfile to use something standard, but still don't get what I expect.

What I'm ultimately trying to accomplish is:

  1. Throw up a docker container
  2. Copy my source code to the container
  3. Run my python script on the container

I've got 1 & 2 working, but not 3. For now I'd be happy if I could just run tests against a container, which is why I modified the Jenkinsfile to be simpler.

My Jenkinsfile:

pipeline {
    agent {
        docker { image 'node:7-alpine' }
    }
    stages {
        stage('Test') {
            steps {
                sh 'pwd'
                sh 'ls -al'
            }
        }
    }
}

When I run docker run node:7-alpine pwd, docker run node:7-alpine ls -al locally, I get the following:

/

and

bin
dev
etc
home
lib
linuxrc
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

But on Jenkins I get:

[test_job] Running shell script
+ pwd
/srv/jenkins/workspace/test_job

and

[test_job] Running shell script
+ ls -al
total 36
drwxr-x---    4 5003     5003          4096 Aug 29 20:44 .
drwxr-xr-x    4 root     root          4096 Aug 29 20:44 ..
drwxr-x---    8 5003     5003          4096 Aug 29 20:44 .git
-rw-r-----    1 5003     5003          1184 Aug 29 19:24 .gitignore
-rw-r-----    1 5003     5003           802 Aug 29 19:47 Dockerfile
-rw-r-----    1 5003     5003           247 Aug 29 20:44 Jenkinsfile
-rw-r-----    1 5003     5003           417 Aug 29 20:24 OriginalJenkinsfile
-rw-r-----    1 5003     5003          2534 Aug 29 19:24 README.md
drwxr-x---    5 5003     5003          4096 Aug 29 19:24 framework

Upvotes: 0

Views: 2141

Answers (1)

herm
herm

Reputation: 16385

As far as I know these commands mount the current directory from jenkins into the container and sets it as workdir. Also there is a bug and you can not switch directory inside the container.

Try ls / which should list whatever is in the root directory of that container

Upvotes: 1

Related Questions