Setting Traefik to intercept traffic between Docker containers

Viewed 64

I have Traefik container (traefik v. 2.8) running as reverse proxy for local development. I also use docker-compose.yml file to define my services.

Also, I have exposed the services present in docker-compose.yml via host.docker.internal by setting these lines in my /etc/hosts file:

127.0.0.1       host.docker.internal
localhost       host.docker.internal

My setup is as such:

  • host.docker.internal:8443 used for service_a
  • host.docker.internal:8453 used for service_b

I have setup Traefik route from a.localhost that goes to host.docker.internal:8443. I can access a.localhost from the host outside the containers just fine, and Traefik does really route the traffic to host.docker.internal:8443 as I want.

Problem is that I have a reason to have service B (host.docker.internal:8453) call service A via the a.localhost hostname. This does not work, as in service B, I get unknown host when trying to access a.localhost

Here is extract from my docker-compose.yml file:

version: '3'
services:
  reverse_proxy:
  image: traefik:v2.8
    # Enables the web UI and tells Traefik to listen to docker
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "443:443"
      # The Web UI (enabled by --api.insecure=true)
      - "9000:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      - ./dev-traefik/traefik.yml:/etc/traefik/traefik.yml
      - ./dev-traefik:/configurations
  
  service_a:
    ports:
      - "8443:8443"

  service_b:
    ports:
      - "8453:8453"

Also I'm using a yml-based configuration for Traefik, present in dynamic-config.yml:

http:
  routers:
    service-a-router:
      service: service-a
      rule: "Host(`a.localhost`)"
      tls: "true" # using tls
  services:
    service-a:
      loadBalancer:
        servers:
          - url: "https://host.docker.internal:8443" # service A

tls:
  certificates:
    - certFile: "/etc/https/tls.crt"
      keyFile: "/etc/https/tls.key"
  stores:
    default:
      defaultCertificate:
        certFile: "/etc/https/tls.crt"
        keyFile: "/etc/https/tls.key"

It seems like Traefik is able to listen to requests made from host network, as accessing https://a.localhost from browser outside the container network works just fine. On the other hand, requests made by service_a container don't seem to be caught by Traefik.

What I have also tried is to add a.localhost to /etc/hosts in the host machine running the containers like this:

127.0.0.1   a.localhost
localhost   a.localhost

And then using curl inside service B container to access service A.

This resulted in getting connection refused as opposed to Could not resolve host: a.localhost. This leads me to suggest that traefik couldn't intercept traffic from service b container

What am I doing wrong? Is there a way to make such setup work? I do have a legit reason for it, which relates to having as close setup as possible in local development as on other environments which are deployed to cloud.

0 Answers
Related