Cannot create redirect for certain service

Hi!

I'm trying to redirect aria.server to caddy, here's what I've arrived at, but it doesn't seem to work. I'm using a Docker setup.

# traefik.yml
http:
  routers:
    aria:
      rule: "Host(`aria.server`)"
      service: caddy-home

  services:
    caddy-home:
      loadBalancer:
        servers:
          - url: "http://caddy"

Hi @selfhoster195 and welcome to the forum.

The yaml file looks good at first sight.
You called it traefik.yml - so is this code snippet part of the static configuration file?
Defining routers and services are part of the dynamic configuration - so this should be defined in an extra file as a part of the file provider.

Another idea - are the docker container 'caddy' and traefik in the same docker network?

Regards,
Wolfgang

Thanks @wollomatic, great to be here.

I’m still not entirely sure about a dynamic or a static configuration, I’m learning more as I go.

This is the compose.yml:

  traefik:
    container_name: traefik
    image: traefik
    restart: unless-stopped
    ports:
      - 80:80
      - 8000:8080
    volumes:
      - /opt/appdata/traefik/traefik.yml:/traefik.yml
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      - traefik.http.routers.traefik.service=api@internal

The created router/service don’t show up in the dashboard.

You've mounted traefik.yml to /traefik.yml in the container.
I assume traefik looks for the config file in /etc/traefik.

I suggest adding a few lines to your configuration:

  1. define an external network. All containers which offer services to be routed with traefik, should be in this same network: docker network create traefik-servicenet
  2. add a dynamic configuration (file provider) to your traefik setup, see example below.

docker-compose.yaml:

services:
  traefik:
    container_name: traefik
    image: traefik:2.5 # version pinning is a good idea
    restart: unless-stopped
    ports:
      - 80:80
      - 8000:8080
    volumes:
      - /opt/appdata/traefik/traefik.yml:/etc/traefik/traefik.yml  # changed the target file path
      - /opt/appdata/traefik/dynamic.yml:/etc/traefik/dynamic.yml
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      - traefik.http.routers.traefik.service=api@internal
    networks:
      - traefik-servicenet

networks:
  traefik-servicenet:
    external: true
    name: traefik-servicenet

traefik.yml:

providers:
  docker:
    endpoint: 'unix:///var/run/docker.sock'
    network: traefik-servicenet
  file:
    filename: /etc/traefik/dynamic.yaml
    watch: true

and then add the routers and the service to the dynamic.yml.

Please keep in mind this is a very basic configuration with no security, so it should not be exposed to the internet.

Regards,
Wolfgang

The difference between the static configuration and the dynamic documenation is:
First, the static configuration is loaded once and cannot be changed in the runtine. It's the configuration of traefik itself.
Then, the dynamic configuration can easily be changed while running, so you can add or delete routers, services, middlewares without restarting traefik.
The dynamic configuration can be done with various ways (different providers), for example:

  • file provider (in this case the dynamic.yml)
  • docker provider (the docker socket, configuration by container labels)
  • kubernetes, ...

Please excuse my bad grammar. English is not my foreign language.

Regards,
Wolfgang

I should've shared the entire configs, but yeah compose does create a discrete network.
Traefik does read /traefik.yml at the root of the container.

I did try using a provider and a separate file ("file provider"), but that still returns 404.

I spoke too early. It works perfectly!

1 Like

However, changes to dynamic.yml show up only after a restart :frowning:

You could try to create a directory dynamic and mount the complete directory in the container.
Then, change the traefik.yml:

file:
    # filename: /etc/traefik/dynamic.yaml   # instead of this line use:
    directory: /etc/traefik/dynamic/

Then, move the dynamic.yml into this directory.
Traefik now watches every file in this directory.

Here's a more complex example: https://github.com/wollomatic/traefik2-hardened/blob/master/config/traefik.yaml

Thanks for the code. Earlier I set the whole /etc/traefik as a file provider directory, setting the file instead worked.

Can I ask you what's the best way to add apps that are on host networking? Do I have to add them manually?

@wollomatic treafik.yaml / traefik.yml is sourced from a few locations:
https://doc.traefik.io/traefik/getting-started/configuration-overview/#configuration-file

1 Like

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