Traefik: Stream dataflow between application does not work

I have the following and basic docker setup where the factory-service creates a continues dataflow which is exposed through Fast API and shall be consumed by another container verification-service. Between both container I want to use Traefik to function as Load Balancer.

I'm able to consume the data stream directly using http://localhost:8001/stream. But unfortunately I'm not able to consume the stream in the verification-service container. I'm stuck with the same issue for weeks now and don't know what to do anymore.. I'm new to Traefik and Docker

Fast API in factory-service

async def stream_data():
    return StreamingResponse(generate_sensor_data(), media_type="text/event-stream")

verification-service


print("Connecting ...")
try:
    response = requests.get("http://factory.local/stream", stream=True)
    print(response.status_code, response.text)
except requests.exceptions.RequestException as e:
    print(f"Error: {e}")

Docker-Compose

  factory-service:
    build:
      context: ./factory
      dockerfile: Dockerfile
    container_name: factory_service
    ports:
      - "8001:8001"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.factory.rule=Host(`factory.local`)"
      - "traefik.http.services.factory.loadbalancer.server.port=8001"
      - "traefik.docker.network=app-network"
    networks:
      app-network:
        aliases:
          - "factory.local"

  verification-service:
    build:
      context: ./verification
      dockerfile: Dockerfile
    container_name: verification_service
    depends_on:
      factory-service:
        condition: service_started
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.verification.rule=Host(`verification.local`)"
      - "traefik.docker.network=app-network"
    networks:
      - app-network

  traefik:
    image: traefik:v3.3
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker"
      - "--providers.docker.exposedbydefault=false"
      - "--entryPoints.web.address=:80"
    ports:
    - "80:80"
    - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
    - app-network

networks:
  app-network:
    driver: bridge
    name: app-network

Use 3 backticks before and after code/config to make it more readable and preserve spacing, which is important in yaml.

Within a container you can not use localhost, as that is only localhost within the container, not on host.

You can add this to your Docker compose files

   extra_hosts:
      - "host.docker.internal:host-gateway"

and try to use host.docker.internal to connect to ports opened on the host directly.

Usually you would use Docker service names or container names to talk to other services/containers within a Docker network, Docker internal DNS resolves to the right IPs.

For internal connections usually Traefik is not involved, what's the purpose of using it for internal requests?

Thank you for the quick response.
Basically I'm building an simulated ETL pipeline within Docker for my studies. The below part is just the extraction and following injection into the Message Broker.
The pipeline needs to be provided and executable on other machines.

Conneting directly to the traefik container in verification-service returns 404 page note found

requests.get("http://traefik/stream", stream=True)

Maybe there is even a better way of achieving this task?

However I'm not very sure how to include the extra_host into my compose file and the get statement