Prova12
Prova12

Reputation: 653

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:

1)

docker build -t my-api-docker:latest .

2)

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

Upvotes: 2

Views: 1228

Answers (1)

Adiii
Adiii

Reputation: 59966

It should not bind with container localhost i.e

Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

change the CMD in Dockerfile

CMD [ "flask", "run", "--host=0.0.0.0" ]

or

CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"] 

or replace the inside your code

app.run(host='0.0.0.0').

Upvotes: 4

Related Questions