I want to configure Traefik to listen on port 80 and then proxy the traffic to my services which are located in docker containers. I don't want to use docker labels, but a single YAML configuration to have all the configurations in a single place.
I have created a docker file like this:
FROM traefik:v3.3
COPY ./traefik.yml /etc/traefik/traefik.yml
EXPOSE 80 8080
then a compose file like below:
services:
proxy:
build:
context: ./proxy
dockerfile: Dockerfile
networks:
- proxy
- frontend
- app
- mongodb_dashboard
- docker_dashboard
deploy:
mode: replicated
replicas: 1
endpoint_mode: vip
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
ports:
- 80:80
- 8080:8080
networks:
proxy:
name: proxy
frontend:
name: frontend
app:
name: app
mongodb_dashboard:
name: mongodb_dashboard
external: true
docker_dashboard:
name: docker_dashboard
external: true
and my Traefik configuration file (traefik.yml) is:
# Define the entry points to listen on port 80 for HTTP
entryPoints:
http:
address: ":80" # Listen on port 80 for HTTP requests
# Enable access logs
log:
level: INFO
format: common
filePath: "/dev/stdout" # Logs to stdout
# Enable the Traefik dashboard (optional, for debugging purposes)
api:
insecure: true # Enable insecure dashboard (for local testing, don't use in production)
dashboard: true # Enable the dashboard view
# HTTP routers, services, and middlewares
http:
routers:
# Backend subdomain
backend:
rule: HostRegexp(`^backend\\..+$`)
entryPoints:
- http
service: app-service
middlewares:
- add-headers
# Docker Dashboard subdomain
dockerdashboard:
rule: HostRegexp(`^dockerdashboard\\..+$`)
entryPoints:
- http
service: docker-dashboard-service
middlewares:
- add-headers
# MongoDB Dashboard subdomain
mongodashboard:
rule: HostRegexp(`^mongodashboard\\..+$`)
entryPoints:
- http
service: mongodb-dashboard-service
middlewares:
- add-headers
# Catch-all router for unmatched subdomains
catch-all:
rule: HostRegexp(`.+`)
entryPoints:
- http
service: frontend-service
middlewares:
- add-headers
services:
frontend-service:
loadBalancer:
servers:
- url: "http://frontend:3000"
app-service:
loadBalancer:
servers:
- url: "http://app:80"
docker-dashboard-service:
loadBalancer:
servers:
- url: "http://docker_dashboard:9000"
mongodb-dashboard-service:
loadBalancer:
servers:
- url: "http://mongodb_dashboard:8081"
middlewares:
# Middleware to add headers to all requests
add-headers:
headers:
customRequestHeaders:
X-Real-IP: {remote_ip}
X-Forwarded-For: {remote_ip}
X-Forwarded-Proto: {scheme}
FIY, the whole project is located: https://github.com/Tarhche/infrastructure/tree/main/proxy
locally, I have setuped a domain named tarhche.loc
like below:
127.0.0.1 tarhche.loc dockerdashboard.tarhche.loc mongodashboard.tarhche.loc
by running Traefik, I always get 404 when I go to each subdomain!
each subdomain is associated with a service, for example mongodashboard.tarhche.loc
should proxy the requests to mongo-express which I run it using: https://github.com/Tarhche/infrastructure/blob/main/compose.mongodb_dashboard.yaml
and even when I go to Traefik dashboard I can't see the defined HTTP services, providers and Middlewares!
could you please give me some insight about how I can solve the issue? btw, the whole code exists in: https://github.com/Tarhche/infrastructure
thanks everyone.
I changed my configuration like below:
traefik.yml
contains below configurations:# Define the entry points to listen on port 80 for HTTP
entryPoints:
http:
address: ":80" # Listen on port 80 for HTTP requests
# Enable access logs
log:
level: DEBUG
format: common
filePath: "/dev/stdout" # Logs to stdout
# Enable the Traefik dashboard (optional, for debugging purposes)
api:
insecure: true # Enable insecure dashboard (for local testing, don't use in production)
dashboard: true # Enable the dashboard view
providers:
file:
directory: /etc/traefik-dynamic
dynamic.yml
contains below configurations:# HTTP routers, services, and middlewares
http:
routers:
# Backend subdomain
backend:
rule: HostRegexp(`^backend\..+\..+$`)
entryPoints:
- http
service: app-service
middlewares:
- add-headers
# Docker Dashboard subdomain
dockerdashboard:
rule: HostRegexp(`^dockerdashboard\..+\..+$`)
entryPoints:
- http
service: docker-dashboard-service
middlewares:
- add-headers
# MongoDB Dashboard subdomain
mongodashboard:
rule: HostRegexp(`^mongodashboard\..+\..+$`)
entryPoints:
- http
service: mongodb-dashboard-service
middlewares:
- add-headers
# Catch-all router for unmatched subdomains
catch-all:
rule: HostRegexp(`.+`)
entryPoints:
- http
service: frontend-service
middlewares:
- add-headers
services:
frontend-service:
loadBalancer:
servers:
- url: "http://frontend:3000"
app-service:
loadBalancer:
servers:
- url: "http://app:80"
docker-dashboard-service:
loadBalancer:
servers:
- url: "http://docker_dashboard:9000"
mongodb-dashboard-service:
loadBalancer:
servers:
- url: "http://mongodb_dashboard:8081"
middlewares:
# Middleware to add headers to all requests
add-headers:
headers:
customRequestHeaders:
X-Real-IP: {remote_ip}
X-Forwarded-For: {remote_ip}
X-Forwarded-Proto: {scheme}
since I use traefik in docker, I configured the docker file like below:
FROM traefik:v3.3
COPY ./traefik.yml /etc/traefik/traefik.yml
COPY ./dynamic.yml /etc/traefik-dynamic/dynamic.yml
EXPOSE 80 8080
then on my /etc/hosts I configured a domain and subdomains:
127.0.0.1 tarhche.loc dockerdashboard.tarhche.loc mongodashboard.tarhche.loc
by opening a subdomain I can see the result.