英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论