Flask App在本地工作,但在本地Docker上不起作用。

huangapple go评论60阅读模式
英文:

Flask App working locally but not working on local docker

问题

以下是您要翻译的内容:

Flask App code:

from flask import Flask,request, url_for, redirect, render_template, jsonify
from pycaret.regression import *
import pandas as pd
import pickle
import numpy as np

app = Flask(__name__)

model = load_model('deployment_28042020')
cols = ['age', 'sex', 'bmi', 'children', 'smoker', 'region']

@app.route('/')
def home():
   return render_template("home.html")

@app.route('/predict',methods=['POST'])
def predict():
    int_features = [x for x in request.form.values()]
    final = np.array(int_features)
    data_unseen = pd.DataFrame([final], columns = cols)
    prediction = predict_model(model, data=data_unseen, round = 0)
    prediction = int(prediction.Label[0])
    return render_template('home.html',pred='Expected Bill will be {}'.format(prediction))

if __name__ == '__main__':
    app.run(debug=True, port=8080, host='0.0.0.0')

Docker file:

 FROM python:3.7
 RUN pip install virtualenv
 ENV VIRTUAL_ENV=/venv
 RUN virtualenv venv -p python3
 ENV PATH="VIRTUAL_ENV/bin:$PATH";

 WORKDIR /app

 COPY requirements.txt requirements.txt

 ADD . /app

# install dependencies
 RUN pip install -r requirements.txt

 COPY . .
 # expose port
 # EXPOSE 5000
 # EXPOSE 8000
 EXPOSE 8080

 # run application
 CMD ["python", "app.py", "--host=0.0.0.0"]
英文:

The app is running locally but when i build docker image and try to run the app from local docker then the browser shows the following error:

This site can’t be reached http://172.17.0.2:8080/ is unreachable.
ERR_ADDRESS_UNREACHABLE and also taking too long to respond

what changes should i make in docker file or in the app code so that i can run it form local docker

Flask App code:

from flask import Flask,request, url_for, redirect, render_template, jsonify
from pycaret.regression import *
import pandas as pd
import pickle
import numpy as np

app = Flask(__name__)

model = load_model('deployment_28042020')
cols = ['age', 'sex', 'bmi', 'children', 'smoker', 'region']

@app.route('/')
def home():
   return render_template("home.html")

@app.route('/predict',methods=['POST'])
def predict():
    int_features = [x for x in request.form.values()]
    final = np.array(int_features)
    data_unseen = pd.DataFrame([final], columns = cols)
    prediction = predict_model(model, data=data_unseen, round = 0)
    prediction = int(prediction.Label[0])
    return render_template('home.html',pred='Expected Bill will be{}'.format(prediction))

if __name__ == '__main__':
    app.run(debug=True, port=8080, host='0.0.0.0')

Docker file:

 FROM python:3.7
 RUN pip install virtualenv
 ENV VIRTUAL_ENV=/venv
 RUN virtualenv venv -p python3
 ENV PATH="VIRTUAL_ENV/bin:$PATH"

 WORKDIR /app

 COPY requirements.txt requirements.txt

 ADD . /app

# install dependencies
 RUN pip install -r requirements.txt


 COPY . .
 # expose port
 # EXPOSE 5000
 # EXPOSE 8000
 EXPOSE 8080

 # run application
 CMD ["python", "app.py", "--host=0.0.0.0"]

答案1

得分: 1

添加一个 docker-compose.yml 文件

version: "3"
services:
  app:
    build: .
    ports:
      - "8080:8080"

运行:docker-compose up --build

需要注意的一点是 172.17.0.2 属于您的容器网络。您可以通过 http://localhost:8080 访问您的网站。

英文:

Add a docker-compose.yml file

version: "3"
services:
  app:
    build: .
    ports:
      - "8080:8080"

Run: docker-compose up --build

An important thing to notice is that 172.17.0.2 belongs to your container network. You can access your site on
http://localhost:8080.

huangapple
  • 本文由 发表于 2023年2月8日 14:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定