hardy6413
hardy6413

Reputation: 11

cannot reach flask application from docker compose

in short my problem is that I cannot reach flask application running inside docker contrainer.

I have flask application

@app.route('/')
def home():
    return "test"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

my dockerfile

# start by pulling the python image
FROM python:3.8

WORKDIR /app
# copy the requirements file into the image
COPY requirements.txt .


RUN pip install -r requirements.txt

# copy every content from the local file to the image
COPY . /app

CMD [ "python", "app.py" ]

my docker compose

version: '3'
services:
  python-backend:
    build: ./LAB3
    container_name: python-backend
    ports:
      - "5000:5000"

folder structure labDocker(contains docker compose) -> LAB3(contains dockerfile)

when I run my application is starting without any errors, see the logs below

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

 * Running on http://127.0.0.1:5000

Press CTRL+C to quit

 * Serving Flask app 'app' (lazy loading)

 * 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

And when I am trying to send request via postman i get error :

GET http://127.0.0.1:5000/
Error: socket hang up
Request Headers
User-Agent: PostmanRuntime/7.31.3
Accept: */*
Postman-Token: e54441e3-9ec4-427c-8202-e09f5cd92500
Host: 127.0.0.1:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

I have browsed through several topics with similar issue on this forum but none of this solved the problem for me, If you have any suggestions please let me know

docker ps -a

CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS                     PORTS                    NAMES
ae6ad71c8542   labdocker_python-backend           "python app.py"          15 minutes ago   Up 15 minutes              0.0.0.0:5000->5000/tcp   python-backend

Upvotes: 0

Views: 187

Answers (1)

hardy6413
hardy6413

Reputation: 11

So the problem was that somehow docker didn't pick up the changes I made do my flask app.py file and didn't have any idea about

app.run(host='0.0.0.0', port=5000)

So I have used this command to rebuild images again

docker-compose build --no-cache python-backend

Now everything works fine

Upvotes: 0

Related Questions