Coder
Coder

Reputation: 37

Flask App working locally but not working on local docker

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"]

Upvotes: 0

Views: 595

Answers (1)

MD Obaidullah Al-Faruk
MD Obaidullah Al-Faruk

Reputation: 233

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.

Upvotes: 1

Related Questions