Skip to main content

Work with traefik

Traefik is a transport proxy that will take care of routing network traffic to the stack applications. We have it connected to docker socket listening to all the changes happening with containers.

To set up http traffic routing we just need to add a few labels to the service described in the stack and Traefik will do the rest.

Select web_hello_world stack and click on Editor tab at the top of the page. Then copy and paste this configuration to the editor and click Update the stack. Do not forget to substitute example.com with you domain name.

version: "2.4"

services:
test:
image: far4599/go-hello-world-http:latest
networks:
- traefik
labels:
traefik.enable: 'true'
# service
traefik.http.services.web_hello_world_svc.loadbalancer.server.port: '8080'
# https
traefik.http.routers.web_hello_world.rule: Host(`hello.example.com`)
traefik.http.routers.web_hello_world.entrypoints: websecure
traefik.http.routers.web_hello_world.service: web_hello_world_svc
traefik.http.routers.web_hello_world.tls.certresolver: myresolver

networks:
traefik:
external: true

Visit https://hello.example.com to see that everything works fine.

Let's look at the labels and figure out what they mean:

  1. traefik.enable: 'true' notifies Traefik that this service is to be monitored by Traefik and the configuration in labels shall be applied.
  2. traefik.http.services.web_hello_world_svc.loadbalancer.server.port: '8080' creates a definition of a service web_hello_world_svc with a load balancer configuration to send all the traffic to the port 8080 of the container.
  3. traefik.http.routers.web_hello_world.rule: Host(``hello.example.com``) creates a definition of a router web_hello_world and instructs Traefik to link the router with the domain name.
  4. traefik.http.routers.web_hello_world.entrypoints: websecure says the router will be accessible only through SSL connection (https://).
  5. traefik.http.routers.web_hello_world.service: web_hello_world_svc links the router to web_hello_world_svc service mentioned above.
  6. traefik.http.routers.web_hello_world.tls.certresolver: myresolver indicates that the router shall use myresolver to get SSL certificate for the domain.

With this simple setup you get outstanding tools with lots of possibilities to boost web development or your personal use.