Back to tutorials
Tutorial

Traefik With Docker Compose for HTTPS on a VPS

Set up Traefik Docker Compose reverse proxy with HTTPS, Let’s Encrypt, and label-based routing on your VPS—end to end.

By Anurag Singh
Updated on Nov 22, 2026
Category: Tutorial
Share article
Traefik With Docker Compose for HTTPS on a VPS

Nginx works. It also gets tedious once you host more than a couple services. Each new container means another server block, another config reload, and another chance to ship a typo.

This walkthrough shows how to run a Traefik Docker Compose reverse proxy on a VPS. Routing lives in container labels. HTTPS comes from Let’s Encrypt automatically. Adding a new subdomain won’t require editing proxy files by hand.

By the end, you’ll have a solid baseline.

You’ll use a dedicated Docker network, a Traefik dashboard that isn’t wide open, HTTP→HTTPS redirects, and Let’s Encrypt certificates.

You’ll also deploy two demo apps on app1.example.com and app2.example.com. Each step includes a quick verification and common fixes for 404s and certificate failures.

What you’ll build (and what you need first)

  • VPS with a public IPv4 address (Ubuntu 24.04 LTS used in commands; notes included for Debian 12 and AlmaLinux/Rocky).
  • Docker + Docker Compose installed.
  • Two DNS records (A/AAAA) pointing to your VPS for app1 and app2 (and optionally traefik).
  • Ports open: 80/tcp and 443/tcp to the VPS.
  • A place to store certificates and logs with correct permissions.

If you need a server for this setup, a HostMyCode VPS is a clean fit. You get root access, predictable networking, and enough CPU/RAM to run Traefik plus your apps comfortably.

If you don’t want to manage OS patching and baseline hardening yourself, consider managed VPS hosting.

Step 1: Point DNS at your VPS (and verify it)

Create DNS records for the subdomains you plan to serve. At minimum:

  • app1.example.com → A record to your VPS IPv4
  • app2.example.com → A record to your VPS IPv4
  • (Optional) traefik.example.com → A record to your VPS IPv4 (only if you want an externally reachable dashboard)

While you’re testing, set TTL to 300 seconds. That helps changes propagate quickly.

If you still need a domain, you can register and manage it via HostMyCode domains.

Verify DNS from your laptop (replace with your domain):

dig +short app1.example.com A

dig +short app2.example.com A

You should get your VPS IP back. If you don’t, stop here and fix DNS first.

Let’s Encrypt won’t issue certificates for names that don’t resolve to your server.

Step 2: Install Docker and Docker Compose on the VPS

On Ubuntu 24.04 / Debian 12, Docker’s official repository is still the safest way to get current engine builds in 2026.

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify:

docker version
docker compose version

AlmaLinux/Rocky note: use dnf to add Docker’s repo and install docker-ce.

Then enable and start the service:

sudo systemctl enable --now docker

Step 3: Basic firewall rules (don’t skip this)

Traefik needs inbound 80/443. SSH should already be allowed.

On Ubuntu/Debian with UFW:

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

On AlmaLinux/Rocky with firewalld:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services

Step 4: Create the Traefik directory layout

Put Traefik’s config and ACME storage in one place. Keep it separate from your applications.

This makes backups easier. It also avoids the “which stack owns this file?” problem later.

sudo mkdir -p /opt/traefik/{data,dynamic,logs}

# ACME file must exist and be private
sudo touch /opt/traefik/data/acme.json
sudo chmod 600 /opt/traefik/data/acme.json

Common mistake: leaving acme.json world-readable.

Traefik will reject it, and certificates won’t persist.

Step 5: Create a shared Docker network for proxying

Traefik can only route to containers it can reach.

Create a dedicated external network. Multiple Compose projects can then attach to it.

docker network create proxy

Verify:

docker network ls | grep proxy

Step 6: Write the Traefik static config (traefik.yml)

Create /opt/traefik/traefik.yml. This is Traefik’s static config.

It defines entrypoints, providers, logging, and the Let’s Encrypt resolver.

sudo nano /opt/traefik/traefik.yml
api:
  dashboard: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
          permanent: true

  websecure:
    address: ":443"

providers:
  docker:
    exposedByDefault: false
  file:
    directory: /etc/traefik/dynamic
    watch: true

certificatesResolvers:
  letsencrypt:
    acme:
      email: admin@example.com
      storage: /etc/traefik/data/acme.json
      httpChallenge:
        entryPoint: web

log:
  level: INFO

accessLog:
  filePath: /etc/traefik/logs/access.log
  bufferingSize: 0

Use a real email for ACME notices (expiry and abuse reports).

This baseline uses the HTTP-01 challenge on port 80. For a typical VPS, it’s the most straightforward option.

Step 7: Lock down the dashboard with basic auth (dynamic config)

The dashboard is useful, but don’t publish it unauthenticated.

It can expose internal service names, router rules, and metadata you don’t want indexed or scraped.

Install apache2-utils (Ubuntu/Debian) to generate an htpasswd hash:

sudo apt-get update
sudo apt-get install -y apache2-utils

Create a username/password hash (you’ll be prompted for a password):

htpasswd -nb admin 'YourStrongPasswordHere'

Copy the output string (it looks like admin:$apr1$...).

Then create /opt/traefik/dynamic/dashboard.yml:

sudo nano /opt/traefik/dynamic/dashboard.yml
http:
  middlewares:
    dashboard-auth:
      basicAuth:
        users:
          - "admin:$apr1$REPLACE_THIS_HASH"

  routers:
    traefik-dashboard:
      rule: "Host(`traefik.example.com`)"
      entryPoints:
        - websecure
      service: api@internal
      middlewares:
        - dashboard-auth
      tls:
        certResolver: letsencrypt

If you don’t want the dashboard reachable from the public internet, don’t create the router.

Keep it private and access it via SSH port-forwarding instead. (More on that in the verification step.)

Step 8: Create docker-compose.yml for Traefik

Create /opt/traefik/docker-compose.yml:

sudo nano /opt/traefik/docker-compose.yml
services:
  traefik:
    image: traefik:v3.2
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /opt/traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      - /opt/traefik/dynamic:/etc/traefik/dynamic:ro
      - /opt/traefik/data:/etc/traefik/data
      - /opt/traefik/logs:/etc/traefik/logs
    networks:
      - proxy

networks:
  proxy:
    external: true

Details that are easy to gloss over, but matter:

  • exposedByDefault: false keeps Traefik from publishing every container you run. You enable routing service-by-service.
  • The Docker socket is mounted read-only. This reduces blast radius if Traefik is compromised.
  • traefik:v3.2 pins a major version. Avoid latest on production.

Start Traefik:

cd /opt/traefik
docker compose up -d
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'

Check logs for ACME and provider errors:

docker logs --since=10m traefik

Step 9: Deploy two demo services with label-based routing

This is where the approach pays off.

With a Traefik Docker Compose reverse proxy, routing rules live next to the service definition. You don’t maintain separate vhost files. You also don’t reload configs.

Create /opt/apps/demo/docker-compose.yml:

sudo mkdir -p /opt/apps/demo
sudo nano /opt/apps/demo/docker-compose.yml
services:
  app1:
    image: traefik/whoami:v1.10
    container_name: demo_app1
    restart: unless-stopped
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.app1.rule=Host(`app1.example.com`)"
      - "traefik.http.routers.app1.entrypoints=websecure"
      - "traefik.http.routers.app1.tls.certresolver=letsencrypt"

  app2:
    image: traefik/whoami:v1.10
    container_name: demo_app2
    restart: unless-stopped
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.app2.rule=Host(`app2.example.com`)"
      - "traefik.http.routers.app2.entrypoints=websecure"
      - "traefik.http.routers.app2.tls.certresolver=letsencrypt"

networks:
  proxy:
    external: true

Start the demo stack:

cd /opt/apps/demo
docker compose up -d
docker ps --format 'table {{.Names}}\t{{.Status}}'

Verify HTTPS from your workstation:

  • Open https://app1.example.com and https://app2.example.com.
  • You should see the whoami output, including request headers and container name.

Verify the certificate:

echo | openssl s_client -servername app1.example.com -connect app1.example.com:443 2>/dev/null | openssl x509 -noout -issuer -subject -dates

Step 10: Dashboard access (two safe options)

If you created traefik.example.com and the dynamic router, browse to:

  • https://traefik.example.com → prompts for basic auth

If you skipped public exposure, keep it that way. For day-to-day ops, logs are often enough.

If you truly want local-only access, use SSH port-forwarding and bind the dashboard to loopback.

Avoid enabling “insecure” modes that people accidentally expose on 0.0.0.0.

Step 11: Add a real app (example: a Node service) without touching Traefik

Most self-hosted apps arrive as a Compose stack. The pattern stays the same.

Join the proxy network. Then add Traefik labels for routing and TLS.

Here’s a skeleton for a Node.js container that listens on port 3000 inside the container:

services:
  myapp:
    image: ghcr.io/yourorg/yourapp:1.2.3
    restart: unless-stopped
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.myapp.rule=Host(`myapp.example.com`)"
      - "traefik.http.routers.myapp.entrypoints=websecure"
      - "traefik.http.routers.myapp.tls.certresolver=letsencrypt"
      - "traefik.http.services.myapp.loadbalancer.server.port=3000"

networks:
  proxy:
    external: true

That last label (loadbalancer.server.port) prevents the classic “container is up, but Traefik returns 502” situation.

If your app doesn’t listen on 80, you must tell Traefik the correct internal port.

Step 12: Production checks you can run in five minutes

Run these before pointing real traffic at the server.

They catch the problems that burn the most time.

  • Ports listening:
    sudo ss -lntp | egrep ':80|:443'
  • Traefik sees Docker:
    docker logs --since=5m traefik | tail -n 100
  • ACME storage writable:
    sudo ls -l /opt/traefik/data/acme.json
    sudo stat /opt/traefik/data/acme.json
  • HTTP redirects to HTTPS:
    curl -I http://app1.example.com | head -n 5
    You want 301 or 308 to https://.
  • Access logs:
    sudo tail -n 20 /opt/traefik/logs/access.log

Common mistakes (and fast fixes)

  • Let’s Encrypt fails with timeout: Your DNS points elsewhere, or ports 80/443 are blocked. Confirm dig output and firewall rules.
  • Traefik serves 404 for a hostname: The router rule doesn’t match. Double-check backticks and spelling in Host(`sub.example.com`).
  • 502 Bad Gateway: Traefik can’t reach the container port. Add traefik.http.services.<name>.loadbalancer.server.port and confirm the app listens on that port.
  • Two Compose projects can’t “see” each other: The app isn’t attached to the external proxy network. Add it and redeploy.
  • Certificates keep re-requesting: Your /opt/traefik/data isn’t persisted or permissions break on redeploy. Ensure it’s a host volume and acme.json is writable by the container.

Backups and restores: protect acme.json and your Compose files

For this setup, two things matter for a fast restore:

  • /opt/traefik (Traefik config + acme.json + logs)
  • Your application Compose directories (for example /opt/apps)

Quick backup to a tarball (store it off the VPS):

sudo tar -czf /root/traefik-and-apps-backup.tar.gz /opt/traefik /opt/apps

Restore test (on a fresh VPS) looks like this:

  1. Install Docker + Compose.
  2. Recreate the network: docker network create proxy
  3. Extract the backup: sudo tar -xzf ... -C /
  4. Start Traefik: cd /opt/traefik && docker compose up -d
  5. Start apps: cd /opt/apps/demo && docker compose up -d

If your broader stack includes databases, don’t rely on filesystem snapshots alone.

Use logical dumps and prove you can restore.

For MySQL/MariaDB, this guide is worth keeping around: Set Up Automatic MySQL Backups With Tested Restores. For PostgreSQL, use: Set Up PostgreSQL Backups With pg_dump + Restore Test.

Monitoring and troubleshooting: what to look at first

When something breaks, don’t guess.

Confirm DNS, confirm ports, then read the logs.

  • Traefik logs:
    docker logs --since=30m traefik | tail -n 200
  • Access log:
    sudo tail -n 100 /opt/traefik/logs/access.log
  • Container health:
    docker ps
    docker inspect --format '{{json .State}}' demo_app1 | jq

If your VPS is out of disk, renewals and container pulls can fail in confusing ways.

This walkthrough helps you clean up without making it worse: Disk Full on VPS: Find Large Files and Free Space Safely.

Optional hardening: automatic container updates (carefully)

Auto-updates are convenient until they aren’t.

If you pin stable images and you’re comfortable with the risk, Watchtower can update containers on a schedule. Read first, then decide: Docker Compose Auto-Updates With Watchtower on a VPS.

Summary: your Traefik baseline for multi-service hosting

You now have Traefik handling HTTPS and label-based routing, backed by a shared external Docker network.

Adding a new service is mostly copy/paste: join proxy, set a hostname rule, and (if needed) specify the internal port.

If you want this setup on infrastructure that stays predictable under real traffic, start with a HostMyCode VPS.

If you’d rather hand off OS updates, security baselines, and operational guardrails, managed VPS hosting is the pragmatic upgrade.

Running multiple Docker apps behind HTTPS goes smoother when your server’s networking is consistent and you have enough headroom for spikes. HostMyCode offers VPS plans that pair well with Traefik + Docker Compose, plus managed VPS hosting if you want the baseline handled without spending your week chasing logs.

FAQ

Do I need a wildcard certificate for Traefik?

No. This tutorial uses HTTP-01 to issue per-host certificates automatically. Wildcards require DNS-01 and API access to your DNS provider.

Can I run Traefik with multiple Compose projects?

Yes. That’s why you created an external Docker network named proxy. Each project joins that network and adds Traefik labels.

Why do I get a 502 from Traefik even though my container is running?

In practice it’s usually the internal port. Add traefik.http.services.<service>.loadbalancer.server.port=<port> and confirm your app listens on that port inside the container.

Is it safe to mount the Docker socket into Traefik?

It’s common, but it’s still a high-value integration. Keep it read-only (as shown), run only trusted images, and don’t expose the dashboard without auth.