Traefik + Docker + OAuth: a free reverse proxy with TSL and Google OAuth2

Advantages of the solution 🕵️‍♂️

  • A single server for all your domains and applications, thanks to the Traefik reverse proxy and its Docker integration
  • HTTPS thanks to the free TLS certificates provided by Let’s Encrypt
  • Private resources protection with Google’s OAuth2
  • Everything automated

What you need 🧰

  • A domain: you need at least a domain for the OAuth service, but you will need one more for each app you want to deploy. Subdomains are fine, most of my services (OAuth included) are on subdomains. Freenom provides free domains if you need a testing one (again: I don’t receive money for them, I just enjoyed the service in the past).

That’s it! ✅

💡PRO TIP: When you see the ☝️emoji, it’s your turn! Follow the instructions.

Prepare the server 🏗

Install docker

Install docker-compose

#!/bin/zsh# get latest docker compose released tag
COMPOSE_VERSION=$(curl -s | grep 'tag_name' | cut -d\" -f4)
# Install docker-compose
sh -c "curl -L${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
chmod +x /usr/local/bin/docker-compose
sh -c "curl -L${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose"
# Output compose version
docker-compose -v

Configure the domain

💡PRO TIP: if you own, you can set configure and * to point to your server, without buying a new domain.

OAuth screen consent 🚧

During the OAuth screen consent, setup choose whatever App name you like (it will be visible during the login in your services) and add to the Authorized domains.

When generating a new OAuth 2.0 Credential pick again the name you want but set as Authorized redirect URIs. This will provide OAuth for * If you have multiple domains just add them in the same way. We will see later how to handle them on the server-side.

Save the Client ID and Client secret for later.

The Traefik stack 🦦

version: "3.9"services:
container_name: traefik
image: traefik
restart: always
stdin_open: true
tty: true
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.yml:/etc/traefik/traefik.yml
- ./acme:/etc/traefik/acme
- "80:80"
- "443:443"
container_name: oauth
image: thomseddon/traefik-forward-auth
restart: always
stdin_open: true
tty: true
traefik.http.middlewares.oauth.forwardauth.address: http://oauth:4181
traefik.http.middlewares.oauth.forwardauth.authResponseHeaders: X-Forwarded-User
traefik.http.middlewares.oauth.forwardauth.trustForwardHeader: "true"
traefik.http.routers.oauth.middlewares: oauth
traefik.enable: "true"
traefik.http.routers.oauth.rule: Host(`oauth.${DOMAIN}`)
traefik.http.routers.oauth.tls: "true"
traefik.http.routers.oauth.tls.certresolver: letsencrypt 4181
name: traefik

This docker-compose will create a stack of two services: traefik and oauth.


We mount the Docker socket, needed to discover the containers and read the labels, the acme directory that we use to store Let’s Encrypt certificates, and the traefik.yml configuration file.

Let’s see how to configure Traefik.
☝️Create a new file traefik.yml with the following content.

# http redirect to https
address: ":80"
to: websecure
scheme: https
# https
address: ":443"
certResolver: letsencrypt
# letsencrypt TLS
storage: /etc/traefik/acme/acme.json
tlsChallenge: {}
entryPoint: web
# listen docker
exposedByDefault: false
network: traefik

First, we create two entrypoints, one for the HTTP (web) that is configured to redirect to the secure entrypoint, and the other for the HTTPS (websecure) configured to use Let’s Encrypt for generating certificates.

Then, we set up Let’s Encrypt as a certificate resolver. It will store certificates in the acme.json file we have mounted. It can use both TLS and HTTP challenge using the web entrypoint.

Finally, we configure Traefik to watch Docker containers. Traefik will connect to containers using the traefik network that we create at the end of the docker-compose.yml file. We will attach this network to each container we want to expose.

OAuth2 with Traefik Forward Auth

We will see the environment variables later on. The OAuth service will run on

The labels configure Traefik to work with the OAuth middleware. In particular:

  • traefik.http.middlewares.oauth sets up a new forwardauth middleware called oauth and configure the headers.
  • traefik.http.routers.oauth.middlewares: oauth activates the Auth Host Mode, which is helpful when dealing with a large number of subdomains.
  • All the other labels are used to proxy to this container. You will use them on each container you want to proxy, but we will see them later.

Note that you will need an OAuth service per each domain (subdomains excluded). For example, the deployment we have seen covers,, and It doesn’t cover nor

This is due to the COOKIE_DOMAIN env variable. The service will cover that domain and all the subdomains of any label. For an additional domain, you need an additional service with its domain specified in COOKIE_DOMAIN.

You can use also subdomain: that is, COOKIE_DOMAIN value can be In this case, it will cover and all the sub-sub-domains (like but not a different subdomain ( is not covered).

Additional configs

☝️Create a .env file from which Docker Compose will load your environment variables.

  • Set DOMAIN to your domain. I assume you have all the subdomain pointing to this server or at least the subdomain called oauth.
  • Insert the Client ID and Secret obtained from Google.
  • SECRET is set to 16 randomly generated digits, but I suggest you put here a randomly generated string with upper and lower case letters, digits, and symbols.

☝️Now you can run the Trefik stack with the following command.

docker-compose --env-file .env up -d

Set up your application 🚀

version: "3.9"services:
# a not exposed service
image: mongo
# a publicly exposed app, using https
image: myapp
- default # to reeach the db
- traefik # to be reached from traefik
# enable traefik and tls using letsencrypt
traefik.enable: "true" "true" letsencrypt
# serve it on Host(``)
# a protected admin panel, accessible using https
image: myadminpanel
- default # to reeach the db
- traefik # to be reached from traefik
# enable traefik and tls using letsencrypt
traefik.enable: "true"
traefik.http.routers.admin.tls: "true"
traefik.http.routers.admin.tls.certresolver: letsencrypt
# serve it on
traefik.http.routers.admin.rule: Host(``)
# protect with oauth
traefik.http.routers.admin.middlewares: oauth
# make the traefik network available for these containers
name: traefik
  1. You need to make the traefik network available to the containers in this file. See the last section of the file.
  2. You need to attach the traefik network to all the containers you want to expose. Docker Compose creates also a default network to connect all the containers inside the docker-compose.yml file. When you attach the traefik network remember to keep the default network too if you need to reach the other containers of the stack.
  3. All the magic is made with labels! traefik.enable: "true" enables Trafik for this container. traefik.http.routers.appname.tls: "true" enables the HTTPS. traefik.http.routers.appname.tls.certresolver: letsencrypt tells Traefik to use Let’s Encrypt for generating TLS certificates.
  4. Use traefik.http.routers.appname.rule: Host(``) to tell Traefik on which domain you want to expose this container. You can specify more than one domain dividing them with a comma: Host(`,`).
  5. If your app exposes only one port, you are good to go. If the app exposes multiple ports you can tell Traefik which use by adding a label: portnumber.
  6. If you want to enable OAuth on that container just add the label traefik.http.routers.appname.middlewares: oauth.

Conclusions 👋

Hey, it doesn’t work!
Don’t be shy!
If you need some help, you find some errors in the code or the instructions, or you have some suggestions, just drop me a message. You can find my contacts on my website.

It works, thanks!
Nice! Would you mind giving me some claps? 👏

Do you want to get in touch? Feel free to contact me. 😉

🔗 — 👨‍💻 Infrastructure Engineer @ BendingSpoons

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store