Traefik is an open-source edge router that intelligently proxies incoming network traffic to other services. You could run a hosting company with it, or run your local apps with .test
domains and full HTTPS.
This project focuses on the simplest steps needed to setup a local Traefik environment so you can start testing with real domain names. Whether for vanity or otherwise.
-
Add
traefik.test
and any desired.test
domains to/etc/hosts
.127.0.0.1 traefik.test 127.0.0.1 my-site.test
-
Create an external Docker network called
web
that will be used to connect traefik to other services (e.g. a Docker container running an nginx image).docker network create web
-
Install
mkcert
(andnss
if you're using Firefox).brew install mkcert brew install nss
-
Setup a local root certificate authority (CA) using
mkcert
.mkcert -install
-
Clone this repository
git clone https://github.com/cwilby/simple-local-traefik.git cd simple-local-traefik
-
Create a TLS certificate/key file for
traefik.test
and store incerts
folder within the repository. Repeat for each of your domains.mkcert -cert-file certs/local.crt -key-file certs/local-key.pem "traefik.test"
-
Start traefik and verify that
https://traefik.test/dashboard/
works. Note that the final/
is mandatory.docker-compose up -d
-
To start a Docker container connected to Traefik:
docker run nginx:latest \ --network web \ --label "traefik.enable=true" \ --label "traefik.docker.network=web" \ --label "traefik.http.routers.my-site.entryPoints=https" \ --label "traefik.http.routers.my-site.rule=Host(`my-site.test`)" \ --label "traefik.http.routers.my-site.tls=true"
-
To do the same in a seperate docker-compose file:
version: "3.7" networks: web: external: true services: my-site: image: nginx:latest networks: - web labels: - traefik.enable=true - traefik.docker.network=web - traefik.http.routers.my-site.entryPoints=https - traefik.http.routers.my-site.rule=Host(`my-site.test`) - traefik.http.routers.my-site.tls=true
You can add a label to your Docker container to set the port, but it's better to let Traefik auto-find the port by looking at the ports exposed by the container.
In other words, if you have a Docker container that exposes port 8080, Traefik will know to use port 8080.
Please submit issues if you think something can be improved, and preferably pull requests if you have the time.