0

Dockerfile

FROM python:3.6.8
COPY . /app
WORKDIR /app
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt install libgl1-mesa-glx -y
RUN apt-get install 'ffmpeg'\
    'libsm6'\
    'libxext6'  -y
RUN pip3 install --upgrade pip

RUN pip3 install opencv-python==4.3.0.38
RUN pip3 install -r requirements.txt
EXPOSE 80
CMD ["python3", "server.py"]

requirements.txt

Flask==0.12
Werkzeug==0.16.1
boto3==1.14.40
torch
torchvision==0.7.0
numpy==1.15.4
sklearn==0.0
scipy==1.2.1
scikit-image==0.14.2
pandas==0.24.2

server.py (Flask Server)

@app.route('/invoke', methods = ['POST'])
def handlePostRequest():
    Insert some log statement here
    return

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80)

Commands which I run

docker build -t test .
docker run -p 5000:5000 test

Then invoked the Docker container using POST request in postman on 0.0.0.0/invoke but I am getting Error: connect ECONNREFUSED 0.0.0.0:80

Please let me know what wrong did I do here?

Falcon
  • 372
  • 4
  • 20

1 Answers1

0

It looks like you are binding to all interfaces with app.run(host="0.0.0.0", port=80)

And it looks like you are mapping host port 5000 to container port 5000 -p 5000:5000

If your process is listening on port 80, you should change the mapping to be -p 5000:80

And then get the IP address of the host where your container is running, and you should be able to: curl <ip>:5000

And that will send traffic to your process in your container listening on port 80

Howard_Roark
  • 4,088
  • 1
  • 14
  • 24
  • Does not work. Even *docker run test* does not work. – Falcon Sep 15 '20 at 13:55
  • Ok well that isn't a lot of information to go on. The issue you're running into is A) you're trying to connect to 0.0.0.0, which doesn't make any sense. You need to connect to your host's ip address and B) Your app is listening on Port 80 but you are binding hostport 5000 to container port 5000 (even though your app doesn't listen on 5000) – Howard_Roark Sep 15 '20 at 13:58
  • The -p 5000:5000 is redundant since I am exposing port 80. 5000 was there cos default port of flask is 5000. I have made it work before using the same command but it stopped working for some reason I dont know. – Falcon Sep 15 '20 at 14:07
  • 1
    That isn’t true. It isn’t redundant. https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker – Howard_Roark Sep 15 '20 at 14:10
  • Thanks for the link, It was due to wrong port configuration. I am not sure of one thing. It worked a week ago on the same container and I am sure about it. That is why I ran the same command from the history. – Falcon Sep 15 '20 at 14:32
  • Conceptually I don't think that's possible unless your app ran on 5000 Anyway glad it's working – Howard_Roark Sep 15 '20 at 14:38