Skip to content

Ehyiah/traefik-docker-local

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

traefik-docker-local

Use traefik in local and automatic discovery for docker projects.

Usage

1 - Create a network

Create a network that will be used by docker projects.

    docker network create traefik

2 - Start traefik

    docker compose up -d

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

3 - Modify your projects docker compose file

in your docker compose file: 2 steps are needed

1. add these labels to exposed service (like caddy, apache, mailer ...)

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:
            - default // you need to add this in order to keep the default network for your stack
            - 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`) || Host(`{SERVICE-URL-2}.docker.localhost`)
            - traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
        networks:
            - default // you need to add this in order to keep the default network for your stack
            - traefik

2. and the networks.

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

networks:
    default: // you need to add this in order to keep the default network for your stack
    traefik:
        external: true

4. Update vhost in your projects

Remember 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}

6. Optional Adding https:

Create certificate

  • mkcert -cert-file certs/local-cert.pem -key-file certs/local-key.pem "docker.localhost" ".docker.localhost" "domain.local" ".domain.local"
  • Reminder: X.509 wildcards only go one level deep, so this won't match a.b.docker.localhost

In your project update labels

    labels:
      - traefik.enable=true // should already be existing il you followed previous steps
      - traefik.http.routers.{SERVICE-NAME}.rule=Host(`project.docker.localhost`) // should already be existing il you followed previous steps
      - traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port=80 // should already be existing il you followed previous steps
      // TLS related lines
      - traefik.http.routers.{SERVICE-NAME}-tls.rule=Host(`project.docker.localhost`) // add these lines on services you want to be https exposed
      - traefik.http.routers.{SERVICE-NAME}-tls.tls=true // add these lines on services you want to be https exposed
      - traefik.http.routers.{SERVICE-NAME}-tls.entrypoints=webSecure // add these lines on services you want to be https exposed

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`)  # Define the router rule "other-service.localhost", the service will be available at this address
            service: caddy-service  # Redirect to the caddy-service
            entryPoints:
                - web  # will use the web entrypoint configured in traefik.yml.

    services:
        caddy-service:
            loadBalancer:
                servers:
                    -   url: "http://localhost:81"  # Caddy listening on localhost:81

and you need to update /etc/hosts as you would usually 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