Docker Community Forums

Share and learn in the Docker community.

Docker is running but I cannot access localhost - Flask application

I built I Flask application and I am trying to make it run on a Docker file.

I run two commands:

docker build -t my-api-docker:latest .
docker run -p 5000:5000 my-api-docker 

Both are run without errors and the output on terminal is the classic Flask:

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

HOWEVER

If I visit: http://localhost:5000 if show that:

The page is not working

requirements.txt

Flask==1.1.1
requests==2.20.1
pandas==0.23.4

Dockerfile

FROM python:3.7
RUN pip install --upgrade pip

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

EXPOSE 5000

CMD [ "flask", "run" ]

Hello
Try with this lines

ENTRYPOINT [ “python” ]

CMD [ “app.py” ]
and build and run this project

docker build -t flask:latest .

docker run -d -p 5000:5000 flask

1 Like

The flask app is serving on localhost:5000 IN the container. this localhost is not the same as your localhost and is not served to the outside. If you want to service to be available outside of the container you must listen to correct ips.
e.g. all:

app.run(host=“0.0.0.0”)

2 Likes

Thank you, it helps me a lot!

Thanks Martin… It worked.