简体   繁体   中英

Build and Run Docker Container in Jenkins

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 ====='                        
            '''
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM