[Python] connection is reestablished when i dockerize a FlaskApp and accessing by my localhost

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I created a python flaskapp. everything works well so I decided to Dockerize it.

    But when I launch my app with docker and try to access to http://localhost:5000/ I get 'The connection has been reset'.

    this is what my docker-compose and my Dockfile look like.

    docker-compose :


    version: '3.8'

    services:
    elasticsearch:
    container_name: elasticsearch
    image: elasticsearch:8.15.2
    ports:
    - "9200:9200"
    - "9300:9300"
    environment:
    - discovery.type=single-node
    - xpack.security.enabled=false
    healthcheck:
    test: ["CMD", "curl", "-f", "http://elasticsearch:9200"]
    interval: 30s
    timeout: 10s
    retries: 5
    networks:
    - mynetwork

    flaskapp:
    container_name: flaskapp
    build: .
    ports:
    - "5000:5000"
    depends_on:
    elasticsearch:
    condition: service_healthy
    networks:
    - mynetwork

    networks:
    mynetwork:
    driver: bridge


    Dockerfile :


    FROM python:3.12

    WORKDIR /IoT

    COPY Pipfile Pipfile.lock /IoT/

    RUN pip install pipenv && pipenv install --deploy --ignore-pipfile

    COPY . /IoT/

    EXPOSE 5000

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


    here is my command which launches docker compose:

    • docker-compose up -d
    Python files that matter:


    # app/__init__.py

    from flask import Flask
    from .routes import main_blueprint
    from elasticsearch import Elasticsearch


    def create_app():
    app = Flask(__name__)

    app.elasticsearch = Elasticsearch(["http://elasticsearch:9200"])

    app.register_blueprint(main_blueprint)

    return app


    # app.py

    from app import create_app

    app = create_app()

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




    when I try from my docker container with the curl command http://localhost:5000 it returns my template index.html, so it works.

    so I tried to connect with the IP of my container and here's how I got it: docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" flaskapp

    but it still doesn't work.

    I understand that my appflask works correctly within my docker but I don't understand why I can't access it via my pc through my localhost.

    thanks for your time and your help !

    Continue reading...

Compartilhe esta Página