Better explanation of router name and host

This commit is contained in:
Andreas Svanberg 2024-10-31 13:42:39 +01:00
parent 169ee2a00c
commit f74bef8ee5

@ -53,12 +53,17 @@ To get Traefik to send traffic to your container you need to inform Traefik what
```
labels:
- "traefik.enable=true"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`${VHOST}`)"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.tls.certresolver=letsencrypt"
- "traefik.http.routers.<router name>.rule=Host(`<host>`)"
- "traefik.http.routers.<router name>.tls.certresolver=letsencrypt"
```
> [!NOTE]
> ${COMPOSE_PROJECT_NAME} and ${VHOST} will be explained in the [next section](#so-what-is-that-compose_project_name-and-vhost)
> &lt;router name&gt; and &lt;host&gt; are placeholders that should be replaced with your own values,
> they are both explained below in their respective sections.
> [!NOTE]
> The example shows a `Host` rule which is the most common but there are many others, see the
> [Traefik documentation](https://doc.traefik.io/traefik/routing/routers/) for more information.
What do all these labels mean? First one tells Traefik that this container should have traffic routed to it. The `rule` says which traffic.
`tls.certresolver` tells Traefik how to generate HTTPS certificate, the only valid value is `letsencrypt`. Unfortunately this has to be specified on each exposed service since there is no way to set a default.
@ -74,12 +79,19 @@ networks:
Not all your services should join this network, but you still want them to be able to communicate with each other. For that you should define a second network used by those services that need to communicate.
### So what is that ${COMPOSE_PROJECT_NAME} and ${VHOST}?
The labels added to your service will define a new Traefik [router](https://doc.traefik.io/traefik/routing/routers/) and each router must have a unique name. [`${COMPOSE_PROJECT_NAME}`](https://docs.docker.com/compose/how-tos/environment-variables/envvars/#compose_project_name) is used since it has a unique value already or there would be conflicts between common service names such as `web` or `db`.
### &lt;router name&gt; in the Traefik labels
This is a unique name that is used to identify the router in Traefik. The name has to be *globally* unique among all
deployed systems, for all repositories and all branches. Fortunately there's an environment variable that is set up for
you named `${COMPOSE_PROJECT_NAME}` that is guaranteed to be unique.
This is used in the [example](#example-compose-file) below. There is rarely, if ever, a need to deviate from this.
`${VHOST}` is the fully qualified hostname that has been generated from your repository and branch name. Generally this is in the form `<repo>-<branch>.branch.dsv.su.se` but this should not be relied upon. What can be relied upon is that it is unique and it is possible to prepend values to generate other valid hosts. This can be used if you want to expose multiple services to the outside world, you can change the `rule` label value to something like ``Host(`api-${VHOST}`)``.
`${COMPOSE_PROJECT_NAME}` can be used for other must be unique values as well, see usage below in the example.
Both of these variables are available in the entire Compose file and can be sent to your services if they are needed.
### &lt;host&gt; in the Traefik labels
If a [`Host` rule](https://doc.traefik.io/traefik/routing/routers/#host-and-hostregexp) is used, the hostname can be
accessed using the environment variable `${VHOST}`. This is a fully qualified hostname that is unique for each
deployment and can be prefixed if there's a need for multiple hosts. Do *not* use `.` in the hostname to create new
subdomains, use `-` instead.
### Example Compose file
This below Compose file consists of three services, a `frontend` that runs in the browser, an `api` that is used by the