-1

my problem is the following

I created a jenkins container thanks to the following Dockerfile

# intall jenkins
FROM jenkins/jenkins:lts

# install python3.8.2 & dependancies
USER root
RUN apt-get update && apt-get -y install build-essential zlib1g-dev libncurses5-dev libgdbm-dev    libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev
RUN curl -O https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
RUN tar -xf Python-3.8.2.tar.xz
RUN cd Python-3.8.2 && pwd && ./configure && make -j 2 && make altinstall && python3.8 --version
RUN python3.8 -m pip install sklearn protobuf requests

# install docker
RUN curl -sSL https://get.docker.com/ | sh

My jenkins is running without problem on localhost:8080

In this jenkins I created a docker pipeline

pipeline{
    agent any
    stages{
            /*stage('retrieve docker images'){
                    steps{
                            sh 'docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
                            sh 'docker pull $model_name'
                    }
            }*/
            stage('create container model'){
                    steps{
                            sh 'docker run -d --name fugazi -t -p 3330:3330 $model_name'
                            sh 'sleep 5'
                    }
            }
            stage('use the model'){
                    steps{
                            sh 'python3.8 LaunchTestModel.py'
                    }
            }
            stage('remove docker stuff'){
                    steps{
                            sh 'docker stop fugazi'
                            sh 'docker rm fugazi'
                            sh 'docker rmi $model_name'
                    }
            }
    }
}

The first and second stages works well, they create a docker container reachable on localhost:3330. Then in third stage I try to communicate with this container, on localhost:3330 , thanks to LaunchTestModel.py.But at this point I receive a connexion refused error.

requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=3330): Max retries exceeded with url: /model/methods/classify (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f492e79db80>: Failed to establish a new connection: [Errno 111] Connection refused'))

If I used LaunchTestModel.py outside jenkins it works well, it send data to localhost:3330 and receive an answer.

It seems that localhost:3330 is not reachable from inside the jenkins container ... So the question is how can I reach localhost:3330 from my jenkins container ? I'm not a docker expert and I searched a while before writing this post and what I think is that it is a matter of network between containers, I tried many things without success. Thanks for your help

Phil
  • 314
  • 1
  • 2
  • 13
  • If you are working on Linux, try using --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. – Cody604 Nov 27 '20 at 11:05
  • Thanks cody. I already tried --network="host", but when you did that the pulished ports are not useable and so I don't know on which port my jenkins is running. Here the warning I received when I tried what you proposed : WARNING: Published ports are discarded when using host network mode – Phil Nov 27 '20 at 12:45
  • `localhost` in Docker almost always means "this container". Either arrange for the two containers to be running on the same Docker network (a little tricky in Jenkins) or use one of the techniques in [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach). The standard Jenkins integration described in [Using Docker with Pipeline](https://www.jenkins.io/doc/book/pipeline/docker/) may also be easier to set up than `docker run` commands. – David Maze Nov 27 '20 at 14:55

1 Answers1

0

I fixed my problem. The error comes from the python script that try to send data at the following adress : http://localhost:3330. As this python script is running inside the Jenkins container, the localhost refers to the localhost of the jenkins container on which nothing is exposed. The container created by the jenkins pipeline script runs on the localhost of the Host not on the localhost of the jenkins container, so I modified this URL by http://172.17.0.1:3330 and it works.

This address 172.17.0.1 is the Jenkins container Gateway, I retrieved it thanks to the following command : docker inspect "jenkins container". Gateway Ip address is located in "Networks settings" section.

Thanks cody604 & David Maze for the time spent to try to help me.

Phil
  • 314
  • 1
  • 2
  • 13