Skip to content

Instantly share code, notes, and snippets.

@steinerkelvin
Last active November 24, 2022 16:41
Show Gist options
  • Save steinerkelvin/647b4a459bf0b7684d0682b0c0a82fc0 to your computer and use it in GitHub Desktop.
Save steinerkelvin/647b4a459bf0b7684d0682b0c0a82fc0 to your computer and use it in GitHub Desktop.
simple TLS/SSL termination with Traefik

simple TLS/SSL termination with Traefik

This is a simple Docker setup of a Traefik container to do SSL/TSL terminantion (you delegate SSL/TLS to another service instead of doing it in the application itself).

It works with applications running on the host (instead of another container) if using the following on Docker Compose:

    extra_hosts:
      - "host.docker.internal:host-gateway"
version: "3.3"
services:
traefik:
container_name: "traefik"
hostname: "traefik"
image: "traefik:v2.6"
command:
- "--configfile=/srv/traefik/traefik.yml"
ports:
- "80:80"
- "443:443"
# - "8080:8080" # Traefik Dashboard
extra_hosts:
- "host.docker.internal:host-gateway" # Only needed to bind Traefik to a service running on the host instead of a container
volumes:
- "/srv/traefik/:/srv/traefik/"
# environment:
# # Needed for the DigitalOcean DNS challenge, in case you can't use the HTTP one.
# # There are other methods, but do not bother if you can serve Traefik on port 80.
# DO_AUTH_TOKEN: "${DO_AUTH_TOKEN}"
http:
routers:
# HTTP
# Can be disabled or redirected with a middleware
# TODO: add redirection config example
to-my-service:
service: my-service
rule: "PathPrefix(`/`)"
entryPoints:
- web
# HTTPS
to-my-service-secure:
service: my-service
rule: "Host(`<my.domain.com>`)"
entryPoints:
- web-secure
tls:
certResolver: lets-encrypt
domains:
- main: "<my.domain.com>"
services:
my-service:
loadBalancer:
servers:
- url: "http://host.docker.internal:8000" # this is redirecting to a port on the host. would be better to redirec to another container
api:
# dashboard: true
# insecure: true
log:
level: DEBUG
accessLog:
filePath: "/srv/traefik/access_log.log"
providers:
# Enable the file provider to define routers / middlewares / services in file
file:
filename: /srv/traefik/site.yml
entryPoints:
web:
address: ":80"
web-secure:
address: ":443"
http:
tls:
certResolver: lets-encrypt
certificatesResolvers:
lets-encrypt:
acme:
email: "[email protected]"
storage: "/srv/traefik/acme.json"
caServer: "https://acme-v02.api.letsencrypt.org/directory" # production (has rate limit. it's pretty high, but you could mess up if testing dozens of times)
caServer: "https://acme-staging-v02.api.letsencrypt.org/directory" # testing
httpChallenge:
entryPoint: web
# # There are other forms to authenticate to issue TLS certificates,
# # e.g. this DigitalOcean DNS one, but do not bother if you can
# # serve traefik on port 80.
# dnsChallenge:
# provider: digitalocean
# delayBeforeCheck: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment