Search code examples
djangodockerjenkinsbackground-process

Running Django server from Jenkins pipeline in Docker container


I am trying to setup a Jenkins pipeline that deploys a Django project and runs the Django's development server in background.

I would like to separate it into 3 steps, Build, Test, Run.

Everything is fine except the last step, indeed, when I setup it like this:

...

stage('Run') {
    steps{
        dir('auto'){
            sh 'pwd'
            sh '/usr/bin/python3.8 manage.py runserver 0.0.0.0:8000'
        }
    }
}

the server starts well, I can access to the project through http://127.0.0.1:8000 but the job is not ending.

I have tried to bypass this issue to have the server running in background using nohup $:

...

stage('Run') {
    steps{
        dir('auto'){
            sh 'pwd'
            sh 'nohup /usr/bin/python3.8 manage.py runserver 0.0.0.0:8000 $'
        }
    }
}

but the server is not reachable at http://127.0.0.1:8000

I am a Jenkins beginner and maybe this is not the right way to have a process running in background.


Solution

  • Following this post: Clean way of launching a shell script in background from Jenkins I have used the variable setup JENKINS_NODE_COOKIE=dontKillMe like this:

    stage('Run') {
        steps{
            dir('auto'){
                sh 'pwd'
                sh 'JENKINS_NODE_COOKIE=dontKillMe nohup /usr/bin/python3.8 manage.py runserver 0.0.0.0:8000 &'
            }
        }
    }