0

I am trying to run a Flask app (which seems to be working fine on the Windows host system) on docker.

This is my app.

from flask import Flask, request
import pickle
import numpy as np

with open("./rf.pkl", "rb") as file:
    model = pickle.load(file)

app = Flask(__name__)

@app.route("/")

def predict():
    s_length = request.args.get("s_length")
    s_width = request.args.get("s_width")
    p_length = request.args.get("p_length")
    p_width = request.args.get("p_width")
    prediction = model.predict(np.array([[s_length, s_width, p_length, p_width]]))
    return str(prediction)

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

As I said, this app is working fine on the host.

And here is my Dockerfile.

FROM continuumio/anaconda3:latest

WORKDIR /home

COPY . /home

RUN pip install -r requirements.txt

EXPOSE 5000

CMD python flask_1.py

When I use docker run -p 5000:5000 rf_api, it says that the app is running on http://0.0.0.0:5000/. However, the browser can't reach the page.

I also tried docker inspect --format '{{ .NetworkSettings.IPAddress }}' <containerid> which says that it is running on 172.17.0.2. But the browser can't reach this IP, either.

I can't figure out what is going wrong. Any help would be appreciated. Thanks!

This is the output from 'docker ps'.

https://i.stack.imgur.com/h6rji.png

prs44
  • 51
  • 3
  • 9

2 Answers2

0

Try to access localhost:5000 or 127.0.0.1:5000 in your browser.

On Windows, run

docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         
default   *        virtualbox   Running   tcp://192.168.33.100:2376

Then try browse that ^^ IP:5000 in the browser.

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • Hi, I am running it on Windows. I did try both of those first before moving to 0.0.0.0:5000. – prs44 Jul 26 '20 at 13:55
  • can you provide the output of `docker ps`(only the container that's running the flask app)? – Neo Anderson Jul 26 '20 at 13:55
  • Sure! I added the image to the original post. – prs44 Jul 26 '20 at 14:01
  • Looks fine... Try to run `docker run -p 9999:80 -d nginx` and see if you can access other published ports. Try to access localhost:9999. You should see the nginx home page – Neo Anderson Jul 26 '20 at 14:06
  • If that works, it means there's a problem in the flask container, although it is unlikely...because you said you can see the `app is running on http://0.0.0.0:5000/` in the logs – Neo Anderson Jul 26 '20 at 14:11
  • Running ```docker run -p 9999:80 -d nginx``` gave me this error. ```C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: driver failed programming external connectivity on endpoint musing_lehmann (a29e5eeda82c92b953ed7ce46a8d30c```. – prs44 Jul 26 '20 at 14:20
  • Very likely, something wrong with the docker engine. That nginx container has no reason to fail. Weird things happen in Windows from time to time. Try to restart docker: https://stackoverflow.com/questions/44414130/docker-on-windows-10-driver-failed-programming-external-connectivity-on-endpoin – Neo Anderson Jul 26 '20 at 14:23
  • Looks like ```docker-machine ls``` is not available by default. I managed to install it on Git Bash. But the command returns a blank table. Also, have tried restarting docker many times. Maybe, I should just give up on trying to use docker from Windows. – prs44 Jul 26 '20 at 14:30
  • It should work on windows as well, although I personally stopped running docker on windows long time ago, I know how it feels and I felt the same frustration spending more time to hack docker instead of focusing on the app :)) – Neo Anderson Jul 26 '20 at 14:37
  • 1
    Will try doing it on an Ubuntu machine later. Thanks for the help. :) – prs44 Jul 26 '20 at 14:39
0

ssh into container Using:

docker exec -it <container name> /bin/bash

Then curl the localhost:5000 if that doesn't work check project logs

  • Doing that gives me this error. """OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"C:/Program Files/Git/usr/bin/bash\": stat C:/Program Files/Git/usr/bin/bash: no such fi le or directory": unknown"" – prs44 Jul 26 '20 at 14:37