Reputation: 1679
I'm trying to build a docker to run a flask app. I've never done this before. I have the flask app working locally. Here is my approach:
My directory structure for the project looks like this:
model.pkl README.md images/ static/
Dockerfile flaskapp.py requirements.txt templates/
I can launch the flask app by running python flaskapp.py
and it runs in my browser (locally).
I want to create a Docker so other machines can run this project without dealing with all the dependency stuff. To do so, I've done the following:
FROM python:3
COPY requirements.txt /tmp
COPY flaskapp.py /tmp
COPY model.pkl /tmp
COPY images /tmp
COPY static /tmp
COPY templates /tmp
WORKDIR /tmp
ADD flaskapp.py /
RUN pip install -r requirements.txt
CMD [ "python", "flaskapp.py" ]
Ran the command docker build -t python-barcode .
That worked, so. I ran docker run python-barcode
. The terminal printed out * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
, but it didn't work and I got this error on the browser:
This site can’t be reached0.0.0.0 refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
So I did some digging and I updated my Dockerfile to this (adding the last line):
FROM python:3
COPY requirements.txt /tmp
COPY flaskapp.py /tmp
COPY model.pkl /tmp
COPY images /tmp
COPY static /tmp
COPY templates /tmp
WORKDIR /tmp
ADD flaskapp.py /
RUN pip install -r requirements.txt
CMD [ "python", "flaskapp.py" ]
CMD ["flask", "run", "--host", "0.0.0.0" ]
docker run python-barcode
again, I get this error:Usage: flask run [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/
How should I proceed?
If its relevant, my flaskapp.py
looks like this:
model = load_learner('', 'model.pkl')
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
def classify(document):
X = document
y = model.predict(X)
return y
class ReviewForm(Form):
pred = TextAreaField('',[validators.DataRequired(),validators.length(min=1)])
@app.route('/')
def index():
form = ReviewForm(request.form)
return render_template('reviewform.html', form=form)
@app.route('/results', methods=['POST'])
def results():
form = ReviewForm(request.form)
if request.method == 'POST' and form.validate():
sequence = request.form['pred']
y = classify(sequence)
return render_template('results.html',
y = y)
return render_template('reviewform.html', form=form)
if __name__ == '__main__':
app.run(host= '0.0.0.0')
Now I am getting this error:
[2020-07-03 00:29:51,222] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "flaskapp.py", line 94, in index
return render_template('reviewform.html', form=form)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python3.8/site-packages/jinja2/loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: reviewform.html
Upvotes: 1
Views: 1092
Reputation: 59896
You can have only CMD
per Dockerfile. so one will likely be ignored.
CMD [ "python", "flaskapp.py" ]
CMD ["flask", "run", "--host", "0.0.0.0" ]
Just remove the second one as you already listening to all interfaces in the code.
CMD [ "python", "flaskapp.py" ]
Now run the Docker container with this command.
docker run -p 5000:5000 -it python-barcode
And then you will able to hit endpoint
htpp://localhost:5000
Upvotes: 1
Reputation: 113930
you need to expose the port to the external world
docker run -p 8000:8000 python-barcode
this maps port 8000 inside the docker container to port 8000 outside the docker container.
then you can go to http://127.0.0.1:8000
on the host computer
you could also redirect the port to a different port
docker run -p 8000:9000 python-barcode
and then access it at http://127.0.0.1:9000
on the host system
Upvotes: 0