Skip to content

config for using traefik in local env and automatic discovery

License

Notifications You must be signed in to change notification settings

Ehyiah/traefik-docker-local

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

traefik-docker-local

Use traefik in local and automatic discovery for docker projects.

Classic Usage

1 - Create a network

First of all create a network that will be used by docker projects.

just type for example :

docker network create traefik

2 - Start traefik

just type

docker compose up -d

then traefik web interface should be available at traefik.docker.localhost

3 - Modify your docker compose file

in your docker compose file : add these labels to exposed service (like caddy, apache, mailer ...) and the network :

Replace {SERVICE-NAME} {SERVICE-URL} and {INTERNAL_PORT_OF_SERVICE} with the correct values.

services:
    webserver:
        image: my-server-image  
        labels:
            - traefik.enable=true
            - traefik.http.routers.{SERVICE-NAME}.rule=Host(`{SERVICE-URL}.docker.localhost`)
            - traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
        networks:
            - traefik

you can have a service being accessible by multiple name :

services:
    webserver:
        image: my-server-image  
        labels:
            - traefik.enable=true
            - traefik.http.routers.{SERVICE-NAME}.rule=Host(`{SERVICE-URL}.docker.localhost`, `{SERVICE-URL-2}.docker.localhost`)
            - traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
        networks:
            - traefik

then and add the network you created in the docker compose file :

networks:
    traefik:
        external: true

4 - Update vhost in your projects

Don't forget to update the vhost of your projects to use the {SERVICE-URL} you mentioned in your compose files.

5 - Access your project

Your project will be accessible via the {SERVICE-URL}.

Serve other web server (e.g caddy, apache, nginx)

If you need to install another web server like caddy for example, just make it work on another port (for example 81 instead of 80 and 441 instead of 443) then in your traefik-dynamic.yml update :

http:
    routers:
        test-router:
            rule: Host(`other-service.localhost`)  # Définir la règle de routage pour l'adresse other-service.localhost
            service: caddy-service  # Rediriger vers le service Caddy
            entryPoints:
                - web  # Traefik utilise l'entrée "web" qui écoute sur le port 80 (configuré dans traefik.yml)

    services:
        caddy-service:
            loadBalancer:
                servers:
                    -   url: "http://localhost:81"  # Caddy écoute sur localhost:81

and you need to update /etc/hosts as you would usualy do without traefik.

About

config for using traefik in local env and automatic discovery

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published