# Communication between two services

**URL:** <https://community.traefik.io/t/communication-between-two-services/20962>\
**Category:** Traefik v2\
**Tags:** docker\
**Created:** [January 3, 2024, 12:47pm UTC](https://community.traefik.io/t/communication-between-two-services/20962 "2024-01-03T12:47:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvoirand](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@tvoirand](https://community.traefik.io/u/tvoirand)\
**Post date:** [January 3, 2024, 12:47pm UTC](https://community.traefik.io/t/communication-between-two-services/20962/1 "2024-01-03T12:47:33Z")

</div>

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.

```auto
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.

```auto
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](https://hub.docker.com/r/voxel51/fiftyone/) to [CVAT](https://github.com/opencv/cvat/blob/develop/docker-compose.yml).  
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!

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [January 3, 2024, 5:49pm UTC](https://community.traefik.io/t/communication-between-two-services/20962/2 "2024-01-03T17:49:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tvoirand](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@tvoirand](https://community.traefik.io/u/tvoirand)\
**Post date:** [January 5, 2024, 10:36am UTC](https://community.traefik.io/t/communication-between-two-services/20962/3 "2024-01-05T10:36:29Z")

</div>

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 `myapi`compose file with its parent folder name.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [January 8, 2024, 10:37am UTC](https://community.traefik.io/t/communication-between-two-services/20962/4 "2024-01-08T10:37:09Z")

</div>

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