Getting containers to run with a Jenkinsfile Getting containers to run with a Jenkinsfile jenkins jenkins

Getting containers to run with a Jenkinsfile


You can run your test on a docker image which has necessary tools to build and test your source code. Then, after building the project, you can create a new docker image.

Let's say you have a java project using maven. In this scenario, test and build your source code. Then, in the next stage, just create your docker image.

IMAGE_ID: the docker image including necessary tools, in the example, maven.

And you need a Dockerfile to issue the command docker build

pipeline {  stages {    stage ('Building') {      steps {        sh '''        docker run -i --rm -v ./:/src -w /src IMAGE_ID  /bin/bash -c "mvn test package"         '''      }    }    stage ('dockerization') {      steps {        sh '''        docker build -t your_tag .        docker push ...        '''      }    }  }}

A minimal Dockerfil example:

FROM openjdk:8-jdkCOPY target/*.jar /app/application.jarEXPOSE 8080ENTRYPOINT java -jar /app/application.jar