Error trying to build docker for flask app
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:

I created a Dockerfile with this inside:

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 [ &quot;python&quot;, &quot;flaskapp.py&quot; ]


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 [ &quot;python&quot;, &quot;flaskapp.py&quot; ]
CMD [&quot;flask&quot;, &quot;run&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot; ]


Then running 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')


EDIT 1
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 &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File &quot;/usr/local/lib/python3.8/site-packages/flask/_compat.py&quot;, line 33, in reraise
    raise value
  File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File &quot;flaskapp.py&quot;, line 94, in index
    return render_template('reviewform.html', form=form)
  File &quot;/usr/local/lib/python3.8/site-packages/flask/templating.py&quot;, line 133, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File &quot;/usr/local/lib/python3.8/site-packages/jinja2/environment.py&quot;, line 930, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File &quot;/usr/local/lib/python3.8/site-packages/jinja2/environment.py&quot;, line 883, in get_template
    return self._load_template(name, self.make_globals(globals))
  File &quot;/usr/local/lib/python3.8/site-packages/jinja2/environment.py&quot;, line 857, in _load_template
    template = self.loader.load(self, name, globals)
  File &quot;/usr/local/lib/python3.8/site-packages/jinja2/loaders.py&quot;, line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File &quot;/usr/local/lib/python3.8/site-packages/flask/templating.py&quot;, line 57, in get_source
    return self._get_source_fast(environment, template)
  File &quot;/usr/local/lib/python3.8/site-packages/flask/templating.py&quot;, line 85, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: reviewform.html


You can have only CMD per Dockerfile. so one will likely be ignored.
CMD [ &quot;python&quot;, &quot;flaskapp.py&quot; ]
CMD [&quot;flask&quot;, &quot;run&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot; ]

Just remove the second one as you already listening to all interfaces in the code.
CMD [ &quot;python&quot;, &quot;flaskapp.py&quot; ]

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
