Search code examples
dockerdocker-composetraefik

Can't access service2.localhost from a different docker container with traefik load balancer


I have a simple setup. 2 BE services in separate containers and traefik in another container as a proxy and load-balancer. Right now everything is running locally on my MacBook and I don't use docker swarm. All of this is run via docker compose.

When I do an API request from service1 towards service2 I get this message:

{
  "message": "connect ECONNREFUSED 127.0.0.1:80",
  "name": "Error",
  "stack": "Error: connect ECONNREFUSED 127.0.0.1:80\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)\n    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)",
  "config": {
    "transitional": {
      "silentJSONParsing": true,
      "forcedJSONParsing": true,
      "clarifyTimeoutError": false
    },
    "transformRequest": [null],
    "transformResponse": [null],
    "timeout": 5000,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1,
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/json",
      "User-Agent": "axios/0.23.0",
      "Content-Length": 121
    },
    "maxRedirects": 5,
    "baseURL": "example.com",
    "method": "post",
    "url": "http://service1.localhost/users",
    "data": "{\"firstname\":\"firstname\",\"lastname\":\"lastname\",\"username\":\"username\",\"email\":\"email@email.com\",\"password\":\"password123!\"}"
  },
  "code": "ECONNREFUSED",
  "status": null
}

From a basic FE call to the service1 API everything is okay, I can also use the swaggers I generated for each API, but I can't make it work between them.

What could it be? Do I need something extra configured on the containers?

Here are my docker-compose.yml files:

service1:

version: '3.8'

services:
    service1:
        build: .
        env_file:
          - .env
        labels:
            - "traefik.enable=true"
            - "traefik.http.routers.service1.rule=Host(`service1.localhost`)"
            - "traefik.http.routers.service1.entrypoints=web"
            - "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
            - "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
            - "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
            - "traefik.http.middlewares.testheader.headers.addvaryheader=true"
            # - "--providers.docker.useBindPortIP=true"
        networks:
            - traefik
            - BE
networks:
    traefik:
        name: traefik_webgatewa
    BE:
        external: true

service2:

version: '3.8'

services:
    service2:
        build: .
        env_file:
            - .env.dev
        labels:
            - "traefik.enable=true"
            - "traefik.http.routers.service2.rule=Host(`service2.localhost`)"
            - "traefik.http.routers.service2.entrypoints=web"
            - "traefik.http.middlewares.testheader.headers.accesscontrolallowmethods=GET,OPTIONS,PUT,POST"
            - "traefik.http.middlewares.testheader.headers.accesscontrolalloworigin=*"
            - "traefik.http.middlewares.testheader.headers.accesscontrolmaxage=100"
            - "traefik.http.middlewares.testheader.headers.addvaryheader=true"
        networks:
            - traefik
            - BE
networks:
    traefik:
        name: traefik_webgateway
    BE:
        external: true

traefik:

version: "3.8"
    services:
        traefik:
            image: "traefik:v2.4"
            container_name: "traefik"
            command:
                - "--log.level=DEBUG"
                - "--api.insecure=true"
                - "--providers.docker=true"
                - "--providers.docker.exposedbydefault=false"
                - "--entrypoints.web.address=:80"
                - "--log.filePath=/traefik.log"
                - "--log.format=json"
                - "--accesslog.filepath=/access.log"
                - "--metrics.prometheus=true"
                - "--metrics.prometheus.buckets=0.1,0.3,1.2,5.0"
                - "--metrics.prometheus.addEntryPointsLabels=true"
                - "--providers.docker.watch"
                - "--metrics.prometheus.addServicesLabels=true"
            ports:
                - "80:80"
                - "8080:8080"
            volumes:
                - "/var/run/docker.sock:/var/run/docker.sock:ro"
            networks:
                - traefik
                - BE
    
    networks:
        traefik:
            name: traefik_webgatewa
        BE:
            external: true

Solution

  • Found this In docker-compose how to create an alias / link to localhost? and Traefik: HTTPS access between applications does not work.

    in the traefik compose yml file I adjusted the networks of the service in order to leverage the DNS from docker.

    networks:
        traefik:
          aliases:
            - fozzie-auth.localhost
            - fozzie-label.localhost
            - fozzie-gateway.localhost