Add example Compose file

This commit is contained in:
Andreas Svanberg 2024-10-31 13:08:14 +01:00
parent a49384214c
commit f9c4ee7f29

@ -72,4 +72,56 @@ The labels added to your service will define a new Traefik [router](https://doc.
`${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}`)``.
Both of these variables are available in the entire Compose file and can be sent to your services if they are needed.
Both of these variables are available in the entire Compose file and can be sent to your services if they are needed.
### 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
frontend and a `db` that is used by the `api`. The `frontend` service is exposed to the outside world, as is the `api`
service as it is meant to be accessed by the application running in the browser. However, it uses a different host than
what is used by the `frontend` and that hostname is passed as an environment variable to the `frontend` so it knows
where it is. The `db` lives in an internal network that it shares with `api` and is not exposed to the outside world.
```yaml
services:
frontend:
build:
context: ./frontend/
depends_on:
- api
networks:
- traefik
environment:
- API_URL=api-${VHOST} # will be something like api-repository-branch.branch.dsv.su.se
labels:
- "traefik.enable=true"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`${VHOST}`)"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.tls.certresolver=letsencrypt"
api:
build:
context: ./api/
depends_on:
- db
networks:
- internal
- traefik
environment:
- DATABASE_HOSTNAME=db # can use service name as hostname since they share the internal network
- FRONTEND_URL=${VHOST} # if needed to configure CORS for example
labels:
- "traefik.enable=true"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`api-${VHOST}`)"
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.tls.certresolver=letsencrypt"
db:
image: mariadb:latest
networks:
- internal
networks:
internal:
name: ${COMPOSE_PROJECT_NAME}_internal
traefik:
name: traefik
external: true
```