Separating services into sub folders disrupts the routing to local hostnames

Hi! I'm a newbie and trying to understand traefik. I have been playing around with their default example as a docker container. In there we're running a whoami service just to demonstrate stuff works. And both the traefik dashboard and the whoami service runs fine. I can even access it on whoami.localhost.

However when I move out my whoami service to its own folder and docker-compose.yml the service is no longer accessible on whoami.localhost and I'd like to understand why that is?

So my setup looks like this:

/traefik/docker-compose-yml

version: "3.3"
services:
  traefik:
    image: "traefik:v2.9"
    container_name: "traefik"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

/whoami/docker-compose-yml

version: "3.3"
services:
  whoami:
    image: "traefik/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
      - "traefik.http.routers.whoami.entrypoints=web"

When separated I can only reach whoami service on a local ip address.

Thanks for helping out.

Hi @meteor. Thanks for your interest in Traefik!

Per default, Docker Compose creates networks per directory.
If you use two directories

  • /traefik/
  • /whoami

You will have two separate networks, and treafik is not “seeing” your whoami container.

What you need to do is to create a new (external) network and use this network.

In short something like:

  • Create an external network with docker network create <network name>
  • In each of your docker-compose. yml configure the default network to use your externally created network with the networks top-level key.
  • You can use either the service name or container name to connect between containers

See the Docker Compose network docs for more information, there are a lot of more settings possible.

1 Like

Thank you so much for pointing me in the right direction! This solved my problem.

I had been trying to figure out the network part for a while, thought it had something to do with it. I created an external network and solved it like this in both my compose files:

...
    networks:
      - traefik
networks:
  traefik:  
    external: true

Feel free to point out any flaws in this. Now I have other problems to tackle but it's part of the learning curve I guess! :smiley: Cheers!

Alternatively you can setup the network in your Traefik docker-compose.yml, but give it a fixed name so it’s independent of compose and always has the same name:

networks:
  proxy:
    name: proxy
1 Like

Thanks! I believe I did something equivalent, but outside the docker-compose:
docker network create traefik

However, with your method I don't have to remember to actually create the network since the compose file will set it up for me. I will consider this. :slight_smile: Cheers!

One thing that bugs me though is that this set-up I've done now works fine on my server. But it is no longer accessible from within my local network. I'm thinking if it has anything with the way I set up my docker network? I can see all my containers within the network I've created.

[
    {
        "Name": "traefikproxy",
        "Id": "010cd28dbc482e04108c4622e5b3216e9193bd05c079bf73e32d922222c00fc9",
        "Created": "2023-03-13T15:03:32.011024717+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.27.0.0/16",
                    "Gateway": "172.27.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3192c0342ca8b2d9dd34bf99d4e889e1b4faacb4e00543f82db3d17baa63f388": {
                "Name": "traefik",
                "EndpointID": "c879a4aa2a31a213e74bb3828345fc2b83cc30890650892d83e04d17cc15f6f8",
                "MacAddress": "0e:6f:f9:7a:dc:09",
                "IPv4Address": "172.27.0.2/16",
                "IPv6Address": ""
            },
            "f9095f8457800b543129e6344a1eeebc414ec23258d5dd3deb0a59547418298b": {
                "Name": "whoami",
                "EndpointID": "03c16a20106c957ae7959505942bcef9f0ca2dbe04c57db6e06329d03953b440",
                "MacAddress": "0e:5f:f9:7a:bc:09",
                "IPv4Address": "172.27.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]


Can you explain what is not working? You can only interact with containers when you are in a container yourself, that is attached to the Docker network.

There is an attachable option for overlay networks which can be set to true to attach other containers that don't belong to a service or a stack defined in the same file, but I think it's only relevant when using Docker Swarm.

I'm sorry if I was being unclear, I'll try explaining.

While my Traefik Dashboard is accessible throughout my local network on it's ip:
http://192.168.1.249:8080/dashboard/ my other services only get a server local ip, that I can't reach from another computer in the network. If that makes sense?

My whoami-compose file:

version: "3.3"
services:
  whoami:
    image: "traefik/whoami"
    container_name: "whoami"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
      - "traefik.http.routers.whoami.entrypoints=web"
    networks:
      - traefikproxy
networks:
  traefikproxy:
    name: traefikproxy
    external: true

I'm not familiar with all the docker/traefik terminology so perhaps I'm not using the right wording.

All my services are accessible on my server. But not from other devices on the network. As is shown in the json above with my docker network inspect of the network the containers are there.

I feel like I'm rambling. Hopefully something makes sense. :slight_smile:

Your routing is done via domain/hostname (like whoami.localhost).

If you want to use that from another computer, you have to make sure that domain/hostname resolves to the correct IP. Either you enter something into your local hosts file or you setup your local DSL router with those DNS entries.

Alternatively you can setup an external DNS server and let a domain point to your local private IP. It will just not be available from outside your network.

Yes, the hostname part I think I understand. However, I'm trying to access the whoami service on its designated IP from traefik. See my screenshot:

On my server, I can access it on 172.30.0.2 but not on other devices.

I solved this by running my server on a VPN with a dedicated IP address. I then assigned a domain name to the IP that is resolved by traefik. Works like a charm!

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