Container to Container communication by host rule

The question I have has been asked before, but has not been answered. For example;

https://community.traefik.io/t/no-route-to-host-from-inside-docker-container/3182

I've configured the host of a couple of nginx containers (with labels) in docker-compose. I can reach these containers from my host machine (Mac with docker-for-mac) because I also changed my host file.

for example;

curl api.foobar.local/ # works on my host machine

However, if I curl the same host from Inside a container (on the same network) I get an connection refused. If I curl the service name its works, meaning they are on the same network.

curl api.foobar.local/arts # connection refused from inside (other) container
curl foobar_api:/foo # 200 response from inside (other) container

I think somehow I will need to add "something" to my docker-compose.yaml so that to the host file of the other_container a new rule will be added. Just like I did on my host machine;

api.foobar.local IP_OF_CONTAINER_ON_NETWORK .

What do I need to add to this?

  foobar_api:
    ... other docker-compose config
    networks:
      - proxy
    labels:
      - traefik.http.routers.osl_api.rule=Host(`api.foobar.local`)
      - traefik.enable=true
      - traefik.docker.network=proxy

  other_container:
    ... other docker-compose config
    networks:
      - proxy
    labels:
      - traefik.http.routers.osl_api.rule=Host(`other-container.local`)
      - traefik.enable=true
      - traefik.docker.network=proxy

I can answer my own question you can alias your container with the hostname on the shared network.

foobar_api:
    ... other docker-compose config
    networks:
      proxy:
         aliases:
             - "api.foobar.local"
    labels:
      - traefik.http.routers.osl_api.rule=Host(`api.foobar.local`)
      - traefik.enable=true
      - traefik.docker.network=proxy

  other_container:
    ... other docker-compose config
    networks:
      - proxy
    labels:
      - traefik.http.routers.osl_api.rule=Host(`other-container.local`)
      - traefik.enable=true
      - traefik.docker.network=proxy
1 Like

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