Communication between two services

Hello,

I am having trouble sending HTTP requests from one service to another through Traefik.

I have two docker-compose.yml files, the first file defines Traefik and a first service (myapi in which I implemented a minimalist FastAPI endpoint), and the second file defines a second service (myclient which also includes a second FastAPI just to keep the container running).

Here is the first docker-compose.yml file, where I defined a web entrypoint for Traefik using port 80, defined 127.0.0.1 as host and 80 as port and web as entrypoint for myapi, and setup a network.

version: '3'

services:

  traefik:
    image: traefik:v2.10
    container_name: traefik
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - mynetwork

  myapi:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: myapi
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.myapi.rule=Host(`127.0.0.1`)"
      - "traefik.http.routers.myapi.entrypoints=web"
      - "traefik.http.services.myapi.loadbalancer.server.port=80"
    networks:
      - mynetwork

networks:
  mynetwork:

Here is the second docker-compose.yml file, where I defined 127.0.0.1 as host and 82 as port and web as entrypoint for myclient, and attached myclient to the Docker network.

services:
  myclient:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: myclient
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.myclient.rule=Host(`127.0.0.1`)"
      - "traefik.http.routers.myclient.entrypoints=web"
      - "traefik.http.services.myclient.loadbalancer.server.port=82"
    networks:
      - myapi_mynetwork

networks:
  myapi_mynetwork:
    external: true

I can curl and send requests from the host machine to myapi using http://127.0.0.1:80. But when trying to curl myapi from myclient container with curl http://127.0.0.1:80, I am getting (7) Failed to connect to 127.0.0.1 port 80 after 0 ms: Couldn't connect to server.

I am new to Traefik and I am obviously making a mistake in my configuration? Any hint would be appreciated!

For information, this is a small reproductible example which imitates my actual use case. This actual use case consists in trying to send annotation requests from FiftyOne to CVAT.
I am trying to use Traefik for this, because CVAT docker-compose.yml already includes Traefik, but I am not even sure this is the right approach?

Many thanks in advance!

Localhost 127.0.0.1 within a container is always the container‘s localhost, not on the host.

To talk to another service/container within a Docker network, you would usually use the service name.

But I am not sure you are using the same Docker network in both compose files.

Thank you for your answer @bluepuma77.
Communication between both services works when using the service name instead of the 127.0.0.1 host I had defined (in my case, from within the myclient container, the correct call is: curl http://myapi:80).
So my case is solved!

But I also noticed that communication works if I remove myclient service from my Traefik configuration (removing the traefik labels from myclient compose file).

Just to make sure I learned the right lesson: is it correct that Traefik is needed to handle communication not between two services deployed on the same Docker network, but from the outside world to my infrastructure?

PS: I am using the same network in both compose files, compose prepends myapicompose file with its parent folder name.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.