0

I have Jenkins on a Centos server and also deployed Docker in same server and established connection. I am trying to deploy Docker image to different production server using Jenkins pipeline.

I already have source code on github with all requirements to deploy.

JENKINFILE (below only small part)

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Running build automation'
                sh './gradlew build --no-daemon'
                archiveArtifacts artifacts: 'dist/trainSchedule.zip'
            }
        }
        stage('Build Docker Image') {
            when {
                branch 'master'
            }
            steps {
                script {
                    app = docker.build("willbla/train-schedule")
                    app.inside {
                        sh 'echo $(curl localhost:8080)'
                    }
                }
            }
        }
        stage('Push Docker Image') {
            when {
                branch 'master'
            }
            steps {
                script {
                    docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') {
                        app.push("${env.BUILD_NUMBER}")
                        app.push("latest")

DOCKER FILE

FROM node:carbon
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]

I am able to build image and deploy but I am confused how we Docker built image with source code COPY package*.json ./ .. To do that it has to be locally on the Jenkins server where my Docker is. I found the images built by command docker image ls and also found build artifacts in /var/lib/jenkins/jobs/<my-job-name>-build/builds/8/archive/target, but where is the source code and how is it copied in Dockerfile?

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
  • `COPY . .` instruction copies the source code. Do you see the source code inside the container after you deploy? – rok Oct 16 '19 at 17:29

1 Answers1

0

When Jenkins runs your code is checked out into a working directory on your jenkins machine. So all of your source files are located on your jenkins box.

When docker build is called, the build directory is the directory of the docker file. That directory is changed with WORKDIR /usr/src/app

Your docker file is doing two copy actions.

COPY package*.json ./
RUN npm install

This will copy the package.json into the docker build image and npm install all of the dependencies (the same way you would on your local machine after a checkout).

then COPY . . will copy all files in the current directory(/usr/src/app) into your docker image being built. This is the call that's actually copying your source code.

Where this is on your jenkins box will depend on your Jenkins setup.

There is some reading on how that is set here How to change workspace and build record Root Directory on Jenkins?

tldr; Based on the snippets above, your source code that docker pulls into your image is {JENKINS_WORKSPACE}/usr/src/app.

Reger05
  • 233
  • 1
  • 12