0

I am building a pretty basic CI/CD pipeline in Jenkins using Docker to run a toy application in Python. I am using the following groovy script

pipeline {
    agent any
    environment {
        def img = ("${env.JOB_NAME}:${env.BUILD_ID}").toLowerCase()
    }   

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/ivan1016017/flaskjenkinspipeline.git'
                sh 'ls -la'
            }
        }
        stage('Build') {
            steps {
                echo "Building our image"
                script {
                    dockerImg = docker.build("${img}")
                }
            }
        }
        stage('Deploy Run') {
            steps {
                echo "Deploy and Run"
                script {
                    cont = docker.image("${img}").run("-d -p 5000:5000")
                }
            }
        }
        stage('Do Some Tests') {
            steps {
                echo "Do Some tests"
            }
        }
    }

}

to run the python application. The Dockerfile that I use to build the image is the following

FROM python:3.10-slim-buster
ADD . /smartapp
WORKDIR /smartapp
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./run.py" ]

If I run the following commands

docker build -t smartapp .
docker run -d -p 5000:5000 smartapp

in the console, the port is reached, and therefore, the app works as expected. However, when I run the pipeline in jenkins, localhost:5000 cannot be reached. What could be the reason?

The following is the Console output in Jenkins

Started by user unknown or anonymous
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline-docker-build-deploy
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] git
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/pipeline-docker-build-deploy/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/ivan1016017/flaskjenkinspipeline.git # timeout=10
Fetching upstream changes from https://github.com/ivan1016017/flaskjenkinspipeline.git
 > git --version # timeout=10
 > git --version # 'git version 2.34.1'
 > git fetch --tags --force --progress -- https://github.com/ivan1016017/flaskjenkinspipeline.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 2ee99b53b0d00053cde6279b0e5d0d902b575692 (refs/remotes/origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 2ee99b53b0d00053cde6279b0e5d0d902b575692 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D main # timeout=10
 > git checkout -b main 2ee99b53b0d00053cde6279b0e5d0d902b575692 # timeout=10
Commit message: "updating files"
First time build. Skipping changelog.
[Pipeline] sh
+ ls -la
total 88
drwxr-xr-x  7 jenkins jenkins 4096 oct 31 07:29 .
drwxr-xr-x 10 jenkins jenkins 4096 oct 30 19:20 ..
-rw-r--r--  1 jenkins jenkins  234 oct 30 19:20 buildDb.py
-rw-r--r--  1 jenkins jenkins  224 oct 30 19:20 Dockerfile
drwxr-xr-x  8 jenkins jenkins 4096 oct 31 08:30 .git
-rw-r--r--  1 jenkins jenkins   66 oct 30 19:20 .gitattributes
-rw-r--r--  1 jenkins jenkins 1799 oct 30 19:20 .gitignore
drwxr-xr-x  2 jenkins jenkins 4096 oct 30 19:20 itests
-rw-r--r--  1 jenkins jenkins 1011 oct 30 20:42 Jenkinsfile
-rw-r--r--  1 jenkins jenkins 2176 oct 30 19:20 JenkinsPipeline3
-rw-r--r--  1 jenkins jenkins  443 oct 30 19:20 jenkinsscript5.sh
-rw-r--r--  1 jenkins jenkins  633 oct 30 19:20 jenkinsscript.sh
-rw-r--r--  1 jenkins jenkins 1072 oct 30 19:20 LICENSE
drwxr-xr-x  4 jenkins jenkins 4096 oct 30 19:20 main
-rw-r--r--  1 jenkins jenkins 2862 oct 30 19:20 Pipeline5
-rw-r--r--  1 jenkins jenkins  122 oct 30 19:20 pytest.ini
-rw-r--r--  1 jenkins jenkins   57 oct 30 19:20 README.md
-rw-r--r--  1 jenkins jenkins  567 oct 30 19:20 requirements.txt
-rw-r--r--  1 jenkins jenkins  291 oct 30 19:20 run.py
-rw-r--r--  1 jenkins jenkins   85 oct 31 07:29 sdfDockerfile_pyagent
drwxr-xr-x  4 jenkins jenkins 4096 oct 30 19:20 tests
drwxr-xr-x  2 jenkins jenkins 4096 oct 30 19:20 utests
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building our image
[Pipeline] echo
This is the img ---------> pipeline-docker-build-deploy:1
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker build -t pipeline-docker-build-deploy:1 .
Sending build context to Docker daemon  227.3kB

Step 1/5 : FROM python:3.10-slim-buster
 ---> 6dcf8d3a28c1
Step 2/5 : ADD . /smartapp
 ---> 4f100bc0953b
Step 3/5 : WORKDIR /smartapp
 ---> Running in 42a6eeeac0d5
Removing intermediate container 42a6eeeac0d5
 ---> dd23854ec7f5
Step 4/5 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Running in 2cebb8c1d30f
Collecting alchemy-mock==0.4.3
  Downloading alchemy_mock-0.4.3-py2.py3-none-any.whl (13 kB)
Collecting atomicwrites==1.4.0
  Downloading atomicwrites-1.4.0-py2.py3-none-any.whl (6.8 kB)
Collecting attrs==21.4.0
  Downloading attrs-21.4.0-py2.py3-none-any.whl (60 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.6/60.6 kB 2.0 MB/s eta 0:00:00
Collecting bcrypt==3.2.0
  Downloading bcrypt-3.2.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.9/61.9 kB 8.1 MB/s eta 0:00:00
Collecting cffi==1.15.0
  Downloading cffi-1.15.0-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (446 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 446.3/446.3 kB 8.5 MB/s eta 0:00:00
Collecting click==8.0.4
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 kB 241.9 MB/s eta 0:00:00
Collecting colorama==0.4.3
  Downloading colorama-0.4.3-py2.py3-none-any.whl (15 kB)
Collecting coverage==6.3.2
  Downloading coverage-6.3.2-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.1/211.1 kB 60.8 MB/s eta 0:00:00
Collecting flake8==4.0.1
  Downloading flake8-4.0.1-py2.py3-none-any.whl (64 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.1/64.1 kB 439.0 MB/s eta 0:00:00
Collecting Flask==2.0.3
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 kB 123.0 MB/s eta 0:00:00
Collecting Flask-Bcrypt==0.7.1
  Downloading Flask-Bcrypt-0.7.1.tar.gz (5.1 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting Flask-Login==0.4.1
  Downloading Flask-Login-0.4.1.tar.gz (14 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting Flask-SQLAlchemy==2.5.1
  Downloading Flask_SQLAlchemy-2.5.1-py2.py3-none-any.whl (17 kB)
Collecting Flask-WTF==0.14.2
  Downloading Flask_WTF-0.14.2-py2.py3-none-any.whl (14 kB)
Collecting greenlet==1.1.2
  Downloading greenlet-1.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (155 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.4/155.4 kB 73.5 MB/s eta 0:00:00
Collecting iniconfig==1.1.1
  Downloading iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)
Collecting itsdangerous==2.1.0
  Downloading itsdangerous-2.1.0-py3-none-any.whl (15 kB)
Collecting Jinja2==3.0.3
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 kB 73.1 MB/s eta 0:00:00
Collecting MarkupSafe==2.1.0
  Downloading MarkupSafe-2.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting marshmallow==3.14.1
  Downloading marshmallow-3.14.1-py3-none-any.whl (47 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.6/47.6 kB 386.3 MB/s eta 0:00:00
Collecting mccabe==0.6.1
  Downloading mccabe-0.6.1-py2.py3-none-any.whl (8.6 kB)
Collecting packaging==21.3
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 358.5 MB/s eta 0:00:00
Collecting pluggy==1.0.0
  Downloading pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting py==1.11.0
  Downloading py-1.11.0-py2.py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.7/98.7 kB 231.9 MB/s eta 0:00:00
Collecting pycodestyle==2.8.0
  Downloading pycodestyle-2.8.0-py2.py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.1/42.1 kB 366.8 MB/s eta 0:00:00
Collecting pycparser==2.18
  Downloading pycparser-2.18.tar.gz (245 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 245.9/245.9 kB 57.0 MB/s eta 0:00:00
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting pyflakes==2.4.0
  Downloading pyflakes-2.4.0-py2.py3-none-any.whl (69 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 69.7/69.7 kB 126.5 MB/s eta 0:00:00
Collecting pyparsing==3.0.7
  Downloading pyparsing-3.0.7-py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.0/98.0 kB 268.3 MB/s eta 0:00:00
Collecting pytest==7.0.1
  Downloading pytest-7.0.1-py3-none-any.whl (296 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 297.0/297.0 kB 59.8 MB/s eta 0:00:00
Collecting pytest-cov==3.0.0
  Downloading pytest_cov-3.0.0-py3-none-any.whl (20 kB)
Collecting six==1.11.0
  Downloading six-1.11.0-py2.py3-none-any.whl (10 kB)
Collecting SQLAlchemy==1.4.31
  Downloading SQLAlchemy-1.4.31-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 43.6 MB/s eta 0:00:00
Collecting tomli==2.0.1
  Downloading tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting Werkzeug==2.0.3
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 kB 248.0 MB/s eta 0:00:00
Collecting WTForms==2.1
  Downloading WTForms-2.1.zip (553 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 553.6/553.6 kB 151.5 MB/s eta 0:00:00
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/site-packages (from alchemy-mock==0.4.3->-r requirements.txt (line 1)) (63.2.0)
Collecting coverage[toml]>=5.2.1
  Downloading coverage-6.5.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (213 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 213.3/213.3 kB 369.7 MB/s eta 0:00:00
  Downloading coverage-6.4.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.5/212.5 kB 329.1 MB/s eta 0:00:00
  Downloading coverage-6.4.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.5/212.5 kB 265.2 MB/s eta 0:00:00
  Downloading coverage-6.4.2-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.3/212.3 kB 289.4 MB/s eta 0:00:00
  Downloading coverage-6.4.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.1/212.1 kB 184.4 MB/s eta 0:00:00
  Downloading coverage-6.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.8/211.8 kB 546.9 MB/s eta 0:00:00
  Downloading coverage-6.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.1/211.1 kB 241.2 MB/s eta 0:00:00
Building wheels for collected packages: Flask-Bcrypt, Flask-Login, pycparser, WTForms
  Building wheel for Flask-Bcrypt (setup.py): started
  Building wheel for Flask-Bcrypt (setup.py): finished with status 'done'
  Created wheel for Flask-Bcrypt: filename=Flask_Bcrypt-0.7.1-py3-none-any.whl size=5032 sha256=559b6dac994f967bd86a2ffff2dc61029febd1f3826a44382cf388c42b840e2d
  Stored in directory: /tmp/pip-ephem-wheel-cache-a2omq7oh/wheels/65/d9/5e/fa9c35de8d654cdad72f939afade72f9d0b9ac35e404a2a98d
  Building wheel for Flask-Login (setup.py): started
  Building wheel for Flask-Login (setup.py): finished with status 'done'
  Created wheel for Flask-Login: filename=Flask_Login-0.4.1-py2.py3-none-any.whl size=15951 sha256=75b3b2575d04faa9ed523cce653c6a5a85b00a140a4f373d5c1c10c224017bdc
  Stored in directory: /tmp/pip-ephem-wheel-cache-a2omq7oh/wheels/e2/06/5a/ef6f7c81bc7bf2c815a34e0ed48319314a1e3b6b7fb77072ae
  Building wheel for pycparser (setup.py): started
  Building wheel for pycparser (setup.py): finished with status 'done'
  Created wheel for pycparser: filename=pycparser-2.18-py2.py3-none-any.whl size=209496 sha256=4fe9a9d30a83eff3a2c8d5a31de7a45bb0db9404e5fe682e253984d654f9cdf3
  Stored in directory: /tmp/pip-ephem-wheel-cache-a2omq7oh/wheels/e3/24/7c/02a60aa35245313f1ebc3510677eaee6ff36e4579ed618b72c
  Building wheel for WTForms (setup.py): started
  Building wheel for WTForms (setup.py): finished with status 'done'
  Created wheel for WTForms: filename=WTForms-2.1-py2.py3-none-any.whl size=140856 sha256=5ab7158763680da7e986cb4e677859005a629312cd393cef431fd8cea6368a05
  Stored in directory: /tmp/pip-ephem-wheel-cache-a2omq7oh/wheels/88/fd/be/aba710c88cc5cecd92af52b2d07191779e7201c578f77d1dc9
Successfully built Flask-Bcrypt Flask-Login pycparser WTForms
Installing collected packages: WTForms, six, pycparser, mccabe, iniconfig, Werkzeug, tomli, pyparsing, pyflakes, pycodestyle, py, pluggy, marshmallow, MarkupSafe, itsdangerous, greenlet, coverage, colorama, click, cffi, attrs, atomicwrites, SQLAlchemy, packaging, Jinja2, flake8, bcrypt, pytest, Flask, alchemy-mock, pytest-cov, Flask-WTF, Flask-SQLAlchemy, Flask-Login, Flask-Bcrypt
Successfully installed Flask-2.0.3 Flask-Bcrypt-0.7.1 Flask-Login-0.4.1 Flask-SQLAlchemy-2.5.1 Flask-WTF-0.14.2 Jinja2-3.0.3 MarkupSafe-2.1.0 SQLAlchemy-1.4.31 WTForms-2.1 Werkzeug-2.0.3 alchemy-mock-0.4.3 atomicwrites-1.4.0 attrs-21.4.0 bcrypt-3.2.0 cffi-1.15.0 click-8.0.4 colorama-0.4.3 coverage-6.3.2 flake8-4.0.1 greenlet-1.1.2 iniconfig-1.1.1 itsdangerous-2.1.0 marshmallow-3.14.1 mccabe-0.6.1 packaging-21.3 pluggy-1.0.0 py-1.11.0 pycodestyle-2.8.0 pycparser-2.18 pyflakes-2.4.0 pyparsing-3.0.7 pytest-7.0.1 pytest-cov-3.0.0 six-1.11.0 tomli-2.0.1
[91mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[0m[91m
[notice] A new release of pip available: 22.2.2 -> 22.3
[notice] To update, run: pip install --upgrade pip
[0mRemoving intermediate container 2cebb8c1d30f
 ---> 284397bb76b7
Step 5/5 : CMD [ "python", "./run.py" ]
 ---> Running in 8f0b6e0ce3d3
Removing intermediate container 8f0b6e0ce3d3
 ---> 401b7b9f2fc2
Successfully built 401b7b9f2fc2
Successfully tagged pipeline-docker-build-deploy:1
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy Run)
[Pipeline] echo
Deploy and Run
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker run -d -d -p 5000:5000 pipeline-docker-build-deploy:1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Do Some Tests)
[Pipeline] echo
Do Some tests
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Thanks in advance

Ivan
  • 119
  • 1
  • 8

0 Answers0