简体   繁体   中英

Flask app running in host system but not in docker

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

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.

ssh into container Using:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM