Docker compose - additional tags to make http rest api endpoints available both at http://localhost and http://api (for internal communication)

Hi,

I'm new to traefik and I'm trying to setup a minimal POC using docker compose.

The goal here is to have two HTTP ReST API endpoints available both inside and outside the docker network.

For now my traefik is setup like this:

# pls ignore the socat stuff
services:
  traefik:
    depends_on:
      - socat
    image: "traefik:v3.3"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.docker.endpoint=tcp://socat:2375"
      - "--entryPoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    environment:
      DOCKER_HOST: socat:2375

and the endpoint 1 and endpoint 2 have the following tags:

  endpoint1:
    image: proj/endpoint1:latest
    # ...
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.endpoint1.rule=Host(`localhost`) && PathPrefix(`/endpoint1`)"
      - "traefik.http.routers.endpoint1.entrypoints=web"
      - "traefik.http.routers.endpoint1internal.rule=Host(`api`) && PathPrefix(`/endpoint1`)"
      - "traefik.http.routers.endpoint1internal.entrypoints=web"

  endpoint2:
    image: proj/endpoint2:latest
    # ...
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.endpoint2.rule=Host(`localhost`) && PathPrefix(`/endpoint2`)"
      - "traefik.http.routers.endpoint2.entrypoints=web"
      - "traefik.http.routers.endpoint2internal.rule=Host(`api`) && PathPrefix(`/endpoint2`)"
      - "traefik.http.routers.endpoint2internal.entrypoints=web"

I presumed that the configuration above was enough to fulfill the following requirements/workflows:

  1. host's machine browser can reach endpoint1 through http://localhost/endpoint1
  2. host's machine browser can reach endpoint2 through http://localhost/endpoint2
  3. the proj-container1-1 container can reach endpoint2 through http://api/endpoint2
  4. the proj-container2-1 container can reach endpoint1 through http://api/endpoint1

but it didn't. at most I was able to two first requirements to work fine (1 and 2) but not the internal connection ones (3 and 4).

the "idea" behind the "http://api" is to help the future containers to not need a list of hardcoded alias like endpoint1, endpoint2, etc...

So what am I missing here to make 3 and 4 work? Or even more important: is possible to make 3 and 4 work?

Thank you for reading :smiley:

api inside container needs to resolve to an IP. If all is connected with a Docker network, you can connect to Traefik container via traefik or add another name to Traefik like api with an alias (doc):

services:
  traefik:
    image: traefik
    networks:
      mynet:
        aliases:
          - api

networks:
  mynet: