0

I need to run docker container in Jenkins so that installed libraries like pycodestyle can be runnable in the following steps.

  1. I successfully built Docker Container (in Dockerfile)
  2. How do I access to the container so that I can use it in the next step? (Please look for >> << code in Build step below)

Thanks

    stage('Build') {
            // Install python libraries from requirements.txt (Check Dockerfile for more detail)


            sh "docker login -u '${DOCKER_USR}' -p '${DOCKER_PSW}' ${DOCKER_REGISTRY}"
            sh "docker build \
                --tag '${DOCKER_REGISTRY}/${DOCKER_TAG}:latest' \
                --build-arg HTTPS_PROXY=${PIP_PROXY} ."
        >>    sh "docker run -ti ${DOCKER_REGISTRY}/${DOCKER_TAG}:latest sh" <<<
            }
    }

    stage('Linting') {
            sh '''
            awd=$(pwd)
            echo '===== Linting START ====='  
            for file in $(find . -name '*.py'); do
                    filename=$(basename $file)
                    if [[ ${file:(-3)} == ".py" ]] && [[ $filename = *"test"* ]] ; then
                            echo "perform PEP8 lint (python pylint blah) for $filename"
                            cd $awd && cd $(dirname "${file}") && pycodestyle "${filename}" 
                    fi
            done
            echo '===== Linting END ====='                        
            '''
    }
merry-go-round
  • 4,533
  • 10
  • 54
  • 102

1 Answers1

2

You need to mount the workspace of your Jenkins job (containing your python project) as volume (see "docker run -v" option) to your container and then run the "next step" build step inside this container. You can do this by providing a shell script as part of your project's source code, which does the "next step" or write this script in a previous build stage.

It would be something like this:

sh "chmod +x build.sh"
sh "docker run -v $WORKSPACE:/workspace ${DOCKER_REGISTRY}/${DOCKER_TAG}:latest /workspace/build.sh"

build.sh is an executable script, which is part of your project's workspace and performans the "next step".

$WORKSPACE is the folder that is used by your jenkins job (normally /var/jenkins_home/jobs//workspace - it is provided by Jenkins as a build variable.

Please note: This solution requires that the Docker daemon is running on the same host as Jenkins! Otherwise the workspace will not be available to your container.

Another solution would be to run Jenkins as Docker container, so you can share the Jenkins home/workspaces easily with the containers you run within your build jobs, like described here:

Running Jenkins tests in Docker containers build from dockerfile in codebase

gclaussn
  • 1,736
  • 16
  • 19
  • But if I put build.sh in Dockerfile, it won't be able to be called in 'Linting' step. – merry-go-round May 10 '19 at 19:56
  • Oh you mean I should call `docker run` in every step then call a script? – merry-go-round May 10 '19 at 19:59
  • How do I find the workspace??? Should I assign it when I'm building Docker? – merry-go-round May 10 '19 at 20:01
  • 1
    yes exactly, you could do the linting in the same way, so that the linting stage just does a sh "docker run .../linting.sh". the workspace is the folder that is used by your jenkins job (normally /var/jenkins_home/jobs//workspace - there is a build variable called $WORKSPACE that references this path. https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables – gclaussn May 10 '19 at 20:01
  • I also got an error of `the input device is not a TTY`. :( – merry-go-round May 10 '19 at 20:06
  • 1
    can you remove the "-it" flags, since it is executed by Jenkins, so no terminal session with stdin is needed – gclaussn May 10 '19 at 20:09
  • `docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/workspace/lint.sh\": permission denied": unknown.` – merry-go-round May 10 '19 at 20:11
  • Interesting... docker is soooo confusing – merry-go-round May 10 '19 at 20:12
  • let me pull and change file permission – merry-go-round May 10 '19 at 20:13
  • you could also do this as part of your stage before you run the docker command: sh "chmod +x lint.sh" – gclaussn May 10 '19 at 20:16
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193168/discussion-between-gclaussn-and-john-baek). – gclaussn May 10 '19 at 20:19