Issue
I am running a Flask application in docker and I cannot get it started. I am working on Ubuntu 18.04.
What happens is that I build the image, I run and then I do in my browser at 127.0.0.1:5000 and get a connection error.
I build the image :
sudo docker build -t flask-image:latest .
Then I run:
sudo docker run -d -p 5000:5000 --name flask_try flask-image:latest
Now, when I go in a browser to 127.0.0.1:5000
, the connection fails.
Then I do sudo docker ps
and I get :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c161375d3a7a flask-image:latest "python app.py" 5 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp flask_try
I did also :
sudo docker logs flask_try
Here I get some warnings related to a library I import, that is all. It seems to me that some warnings are not a problem. Does someone think otherwise ?
I did also :
sudo docker exec -it flask_try /bin/bash
And I get the error "container is not running" :
Error response from daemon: Container b20b12ac58d461e5e60c5da1873585f03ba2c8c7a2c3d3408e4e46882e2f69d3 is not running
Here is what my Dockerfile looks like :
FROM python:3.7 as alias_one
RUN apt-get clean && apt-get update
RUN apt-get install -y \
software-properties-common \
python3.7 \
python3-pip \
wget \
apt-transport-https \
build-essential \
ca-certificates \
bash \
perl \
publicsuffix
RUN mkdir /local/
WORKDIR /local/
(... install libs with wget, run installation shells etc. ...)
# second image:
FROM python:3.7
# new workdir
WORKDIR /flask_app_dir
ADD . /flask_app_dir
ARG SPACY_VERSION=2.3.4
COPY ./dir1 /flask_app_dir/dir1
COPY ./__init__.py /flask_app_dir/__init__.py
COPY ./config.py /flask_app_dir/config.py
COPY ./app.py /flask_app_dir/app.py
COPY ./requirements.txt /pynlg_root/requirements.txt
COPY --from=alias_one /local/ /local/
# I declare here a path I need
ENV PATH /local/bin:/local/cmd:$PATH
RUN pip install -r requirements.txt
RUN pip install spacy==${SPACY_VERSION}
RUN python -m spacy download fr_core_news_lg
RUN pip install six
ENTRYPOINT ["python"]
CMD ["app.py"]
The file requirements.txt contains one line :
flask==1.1.2
My application (file app.py) is this. Note: this Flask application works well without Docker.
# encoding: utf-8
from flask import Flask
app = Flask(__name__)
( ... imports ...)
@app.route('/', methods=['GET', 'POST'])
def parse_request():
( ... run some code that does something ...)
return_dict = {
"key1":"value1"
"key2":"value2"
}
return return_dict
if __name__ == '__main_':
app.run(debug=True, host='0.0.0.0', port=5000)
Any help is welcome !
Solution
I solved my problem. In fact I had to delete from my Dockerfile:
ENTRYPOINT ["python"]
CMD ["app.py"]
And replace it just with :
CMD ["flask", "run", "--host", "0.0.0.0"]
This allowed to see the result in the browser.
Answered By - Catalina Chircu