If you run self-hosted apps and want HTTPS without the complexity, learning how to set up Traefik with Docker Compose is one of the best things you can do for your stack. Traefik acts as a reverse proxy that sits in front of your containers, handles routing by reading Docker labels, and automatically gets free SSL certificates from Let's Encrypt.
This guide will show you how to set up Traefik with Docker Compose on Ubuntu, and you will get automatic HTTPS, proper folder structure, a working sample app, and basic hardening, all without Kubernetes.
Prerequisites to Set Up Traefik with Docker Compose
Before you begin to set up Traefik with Docker Compose, make sure to prepare these prerequisites:
You can use the command below to generate the password hash and then replace it in the above config. Remember to double every $ sign when pasting into a compose file:
If you need Apache utils, you can install them first:
Bash
aptinstallapache2-utils-y
Explore key settings in the Traefik Docker Compose file:
Setting
What It Does
security_opt: no-new-privileges:true
Prevents container processes from gaining extra Linux privileges.
providers.docker.exposedbydefault=false
Traefik ignores containers unless they have traefik.enable=true.
providers.docker.network=traefik-proxy
Traefik only reads the shared proxy network, not all container networks.
tlschallenge=true
Uses TLS-ALPN-01 challenge for certificates (port 443 only, no port 80 needed for cert).
/var/run/docker.sock:ro
Mount as read-only, Traefik only needs to read Docker events.
HTTP → HTTPS redirect
All port 80 traffic automatically redirects to 443.
Step 3: Start Traefik
Once you are finished creating the Docker Compose, you can use the command below to bring up the Traefik stack:
Bash
cd/opt/traefikdockercomposeup-d
Check that it started cleanly with the command below:
Bash
dockerlogstraefik--tail30
You should see lines like Configuration loaded from flags and no errors. After 30 to 60 seconds, visit:
HTML/XML
https://traefik.yourdomain.com
You should get a login prompt for the dashboard.
Step 4: Deploy a Sample App with Traefik Whoami
Now you will see why it is so easy to set up Traefik with Docker Compose for multiple apps. Each new app just needs Docker labels and no Traefik config edits required.
For example, create the whoami app compose file with the command below:
Bash
nano/opt/apps/whoami/docker-compose.yml
Add the following content with your actual subdomain and make sure its A record points to your server IP:
Traefik will request a certificate automatically, and you will see the whoami output with your request headers.
How Docker Labels Work for Traefik
When you set up Traefik with Docker Compose, each label tells Traefik something specific about how to route traffic. Here are the labels and what they do:
1. Tell Traefik to watch this container:
HTML/XML
traefik.enable=true
2. Route requests for this hostname to this container:
Note: You do not publish ports on the app container; only Traefik exposes ports 80 and 443. App containers expose only their ports for internal Docker networking.
Verify Your Traefik Setup with Docker Compose
When you are finished setting up Traefik with Docker Compose, you must verify everything works correctly.
Open the following URL in your browser, log in with the credentials you set up, and you should see your routers and services listed under the HTTP section:
HTML/XML
https://traefik.yourdomain.com
4. Check Traefik logs with:
Bash
dockerlogstraefik--follow
Look for any level=error lines. A healthy setup shows only level=info or level=debug messages.
Basic Hardening Tips To Set Up Traefik with Docker Compose for Production
When you set up Traefik with Docker Compose for production, these hardening steps matter a lot:
Never expose port 8080: This is the insecure dashboard port. This guide routes the dashboard through HTTPS with basic auth instead.
Mount the Docker socket as read-only, :ro at the end of the volume line: Traefik only needs to watch events, not control Docker.
Use security_opt: no-new-privileges:true: Stops container processes from escalating privileges.
Set exposedbydefault=false: Prevents accidentally exposing containers without labels.
Keep Traefik updated: The project releases security patches regularly. Pin to a minor version like traefik:v3.7 and update on new releases.
Use strong passwords for basic auth and consider replacing it with an SSO solution like Authentik or Authelia for the dashboard.
Enable access logs: The --accesslog=true gives you visibility into all traffic hitting your proxy.
How To Add More Apps to Traefik
The power of this setup is that adding a new self-hosted app is just a matter of joining it to the Traefik Proxy network and adding the right labels. Here is the template for any new service:
YAML
1networks:2traefik-proxy:3external:true45services:6myapp:7image: myapp:latest8container_name: myapp9restart: unless-stopped10expose:11-"3000"# the port your app listens on internally12networks:13- traefik-proxy14labels:15- traefik.enable=true16- traefik.http.routers.myapp.rule=Host(`myapp.yourdomain.com`)17- traefik.http.routers.myapp.entrypoints=websecure18- traefik.http.routers.myapp.tls.certresolver=letsencrypt19- traefik.http.services.myapp.loadbalancer.server.port=3000
Note: No changes to the Traefik container needed; it detects new containers automatically.
Conclusion
With Traefik Docker Compose setup, you get automatic HTTPS and dynamic routing through Docker labels, all without touching Nginx configs or managing certificates manually. Once the traefik-proxy network is ready and Traefik is running, adding a new app takes less than a minute.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.