Skip to main content

How to Deploy Services Behind Traefik

·282 words·2 mins

This guide assumes you have already followed my guide on installing Traefik in Docker, if you haven’t already, you should probably go check it out.

There are two main ways to put services behind Traefik:

This guide will cover both.

Docker labels
#

1. The compose file
#

Here is the container we will be putting behind Traefik:

---
services:
  nginx:
    image: nginx:latest
    volumes:
      - ./public:/usr/share/nginx/html:ro
    ports:
      - 80:80
    restart: unless-stopped

2. Adding our container to the traefik network
#

---
services:
  nginx:
    image: nginx:latest
    volumes:
      - ./public:/usr/share/nginx/html:ro
    networks: # add container to the traefik network
      - traefik
    restart: unless-stopped

networks:
  traefik: # define the network
    external: true # means the network will not be created by this compose project

I like to remove the web port(s) from containers I put behind Traefik so that is what I have done in the example above.

3. Adding labels
#

---
services:
  nginx:
    image: nginx:latest
    volumes:
      - ./public:/usr/share/nginx/html:ro
    labels:
      - traefik.enable=true
      - traefik.http.services.nginx-example.loadbalancer.server.port=80 # replace with the port this application uses
      - traefik.http.routers.nginx-example.entrypoints=websecure
      - traefik.http.routers.nginx-example.rule=Host(`nginx-example.yourdomain.here`) # replace with your domain for this application
      - traefik.http.routers.nginx-example.tls=true
      - traefik.http.routers.nginx-example.tls.certresolver=letsencrypt-prod
    networks:
      - traefik
    restart: unless-stopped

networks:
  traefik:
    external: true

Config files
#

If you want to proxy something that is not hosted in Docker, such as TrueNAS, you can use config files.

Here is an example for proxying truenas.internal.yourdomain.here to 10.77.0.10

# /opt/traefik/config/truenas.yaml
http:
  routers:
    truenas:
      rule: "Host(`truenas.internal.yourdomain.here`)"
      service: truenas
      entryPoints:
        - web
        - websecure
      tls:
        certResolver: letsencrypt-prod

  services:
    truenas:
      loadBalancer:
        servers:
          - url: "http://10.77.0.10:80"

Done
#

That’s all there is to do, Traefik will now proxy for whatever application you want. Remember to point the DNS for the domain to your server.

DontDDoS
Author
DontDDoS
KUBERNETES!!!