Resolve Same URL on Host and Inside Container

Greetings,

I have traefik set up similar to the docs:

version: "3.9"
services:
  whoami:
    image: "traefik/whoami"
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.tls=true
      - traefik.http.routers.whoami.rule=Host("whoami.docker.localhost")
    networks:
      - net

I have another container that is on the same network, but it cannot resolve to the whoami container as listed above whoami.docker.localhost ... it only resolves as simply whoami. Is there a way for my second container to be able to resolve whoami.docker.localhost?

Here is configuration for second container:

  iam:
    build:
      context: .
      dockerfile: ./Dockerfile.iam
    restart: "always"
    environment:
      ENABLE_CORS: true
      ASPNETCORE_ENVIRONMENT: local
    labels:
      - traefik.enable=true
      - traefik.http.routers.iam.tls=true
      - traefik.http.routers.iam.rule=Host("iam.docker.localhost")
    networks:
      - net

For more reference:

root@877240e2e82c:/app# curl whoami.docker.localhost
curl: (6) Could not resolve host: whoami.docker.localhost
root@877240e2e82c:/app# curl whoami
Hostname: d018699e36e5
IP: 127.0.0.1
IP: 172.18.0.8
RemoteAddr: 172.18.0.6:38320
GET / HTTP/1.1
Host: whoami
User-Agent: curl/7.74.0
Accept: */*

First, for the rule=Host() you need to use backticks around the domain name.

If you want a dedicated domain name to be resolved to a container (within another container), then you could use Docker network alias on the target container.

In general, if your curl can not resolve a domain name, it has nothing to do with Traefik. You have to set up the domain name in DNS or your local hosts file to point to an IP address. If you expect domain.docker.localhost to work automatically, then you need to check the Docker docs, how and if Docker handles this.

Adding the network alias did the trick - thank you!

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