How to Run a Home‑Lab MQTT Broker with Traefik and ACME on Debian

Setting Up a Home‑Lab MQTT Broker with Traefik and ACME on Debian

MQTT is the de‑facto messaging protocol for IoT and home‑automation projects.
If you already run a small lab or a home server, adding a broker that is secure, automatically cert‑managed, and easily reachable from the internet is a great way to learn about reverse proxies, TLS, and container orchestration.

Below is a step‑by‑step guide that uses Docker Compose (or Podman‑Compose if you prefer a rootless setup), Traefik as the reverse proxy, and Let’s Encrypt (ACME) for free TLS certificates.
Everything runs on a fresh Debian 12 “bookworm” installation, but the concepts translate to other distros with little change.

Why Traefik?
Traefik is lightweight, auto‑discovers services via Docker labels, and has built‑in ACME support. It’s a common choice in home‑lab stacks because it keeps the broker invisible to the public and lets you expose only the ports you need.


1. Prerequisites

Item What you need Why
Debian 12 host 2 GB RAM, 20 GB disk Minimal but sufficient for a broker + Traefik
Root or sudo access To install packages and configure networking
Public domain e.g. mqtt.example.com Let’s Encrypt needs a domain that resolves to your public IP
Port forwarding Forward 443 (HTTPS) to the host Traefik listens on 443 for TLS
Docker (or Podman) 20 MB download Container runtime
Docker‑Compose 5 MB download Compose file syntax
Basic knowledge of the terminal

Tip: If you’re on a cloud VPS, the public IP is already exposed.
If you’re behind a home router, make sure the router forwards TCP 443 to the Debian host.


2. Install Docker and Docker‑Compose

Debian’s official Docker repository is the most straightforward path.

# Update and install prerequisites
sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release

# Add Docker’s GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add the repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Verify
docker run --rm hello-world

Install Docker‑Compose (the standalone binary is the simplest):

sudo curl -L "https://github.com/docker/compose/releases/download/v2.29.2/docker-compose-$(uname -s)-$(uname -m)" \
  -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version

Alternative: If you prefer rootless containers, install Podman and Podman‑Compose. The Compose file syntax is identical, and Podman automatically runs as the current user.


3. Prepare the Directory Structure

Create a project folder and a basic docker-compose.yml. All configuration files will live here.

mkdir -p ~/mqtt-lab/{traefik,mosquitto}
cd ~/mqtt-lab

4. Traefik Configuration

Traefik needs two files: a static config (traefik.yml) and a dynamic config (traefik-dynamic.yml).
We’ll also create a folder for ACME certificates.

# Static config – tells Traefik how to run
cat > traefik/traefik.yml <<'EOF'
entryPoints:
  web:
    address: ":80"
  websecure:
    address: ":443"

providers:
  docker:
    exposedByDefault: false

certificatesResolvers:
  letsencrypt:
    acme:
      email: [email protected]
      storage: /acme/acme.json
      httpChallenge:
        # Traefik will use HTTP‑01 challenge on port 80
        entryPoint: web
EOF
# Dynamic config – defines routers, services, and middlewares
cat > traefik/traefik-dynamic.yml <<'EOF'
http:
  routers:
    mqtt:
      rule: "Host(`mqtt.example.com`)"
      entryPoints: ["websecure"]
      service: mqtt
      tls:
        certResolver: letsencrypt
        domains:
          - main: mqtt.example.com
            sans: []
  services:
    mqtt:
      loadBalancer:
        servers:
          - url: "http://mosquitto:1883"
EOF

Why HTTP‑01?
The HTTP challenge is the simplest to set up because Traefik can serve the challenge file on port 80 without needing DNS‑01 records.
If you prefer DNS‑01 (e.g., for wildcard certs), you’d need a DNS provider that Traefik can authenticate with.

Create the ACME storage file and set permissions:

mkdir -p traefik/acme
touch traefik/acme/acme.json
chmod 600 traefik/acme/acme.json

5. Mosquitto (MQTT Broker) Configuration

We’ll use the official Mosquitto image, but override the default config to enable TLS and authentication.

# mosquitto.conf – minimal, but secure
cat > mosquitto/mosquitto.conf <<'EOF'
pid_file /var/run/mosquitto/mosquitto.pid
user mosquitto
listener 1883
protocol mqtt
allow_anonymous false
password_file /mosquitto/config/passwords
EOF

Create a simple password file (replace user1 and pass1 with real credentials):

# Install mosquitto-clients to generate password hash
sudo apt install -y mosquitto-clients
mosquitto_passwd -c mosquitto/passwords user1
# You’ll be prompted for a password; enter `pass1`

Security note: allow_anonymous false forces clients to authenticate.
For a public broker, you might enable anonymous access but restrict topics.


6. Docker‑Compose File

Now we tie everything together.

cat > docker-compose.yml <<'EOF'
version: "3.8"

services:
  traefik:
    image: traefik:v3.1
    container_name: traefik
    command:
      - --configFile=/etc/traefik/traefik.yml
      - --providers.file.filename=/etc/traefik/dynamic.yml
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      - ./traefik/traefik-dynamic.yml:/etc/traefik/dynamic.yml:ro
      - ./traefik/acme:/acme
      - /var/run/docker.sock:/var/run/docker.sock:ro
    restart: unless-stopped

  mosquitto:
    image: eclipse-mosquitto:2
    container_name: mosquitto
    volumes:
      - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
      - ./mosquitto/passwords:/mosquitto/config/passwords:ro
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.mqtt.rule=Host(`mqtt.example.com`)"
      - "traefik.http.routers.mqtt.entrypoints=websecure"
      - "traefik.http.routers.mqtt.tls.certresolver=letsencrypt"
      - "traefik.http.services.mqtt.loadbalancer.server.port=1883"
    restart: unless-stopped
EOF

Why labels?
Traefik reads Docker labels to auto‑configure routes.
The labels above duplicate the static config but are more flexible if you later add multiple brokers.


7. Bring the Stack Up

docker-compose up -d

Check the logs for Traefik:

docker logs -f traefik

You should see a line similar to:

[2026-12-02 12:34:56] INFO: Traefik is ready to accept connections

If the ACME challenge fails, Traefik will log an error. Common issues:

Problem Fix
404 Not Found on /.well-known/acme-challenge/… Ensure port 80 is reachable from the internet.
Permission denied on acme.json Verify chmod 600 and correct ownership.
DNS not pointing to your IP Double‑check the A record.

Once the challenge succeeds, Traefik will write a certificate to acme/acme.json. You can inspect it:

openssl x509 -in <(docker exec traefik cat /acme/acme.json | jq -r '.Certificates[0].Certificate[0] | @base64d') -text -noout

8. Testing the MQTT Connection

From a client machine (or the host itself), use mosquitto_pub and mosquitto_sub with TLS.

# Subscribe (replace user1/pass1)
mosquitto_sub -h mqtt.example.com -p 8883 -u user1 -P pass1 -t test/topic -d
# In another terminal, publish
mosquitto_pub -h mqtt.example.com -p 8883 -u user1 -P pass1 -t test/topic -m "Hello, MQTT!"

If you see the message, the broker is reachable over TLS.
If you get a “Connection refused” error, double‑check:

  • The broker is listening on 1883 inside the container.
  • Traefik is forwarding to that port.
  • The client is using port 8883 (the default TLS port for MQTT).
    Traefik forwards plain HTTP to 1883, but the client should connect over TLS to 8883.

9. Exposing MQTT over WebSocket (Optional)

Some clients (e.g., web dashboards) prefer WebSocket.
Add a WebSocket listener to Mosquitto:

# Add to mosquitto.conf
listener 9001
protocol websockets

Update the Traefik router to expose port 9001:

labels:
  - "traefik.http.services.mqtt.loadbalancer.server.port=9001"

Restart the stack:

docker-compose down
docker-compose up -d

Now you can connect from a browser:

const client = mqtt.connect('wss://mqtt.example.com:9001', {
  username: 'user1',
  password: 'pass1',
});

10. Managing Certificates

Traefik automatically renews certificates 30 days before expiry.
You can force a renewal by editing the traefik.yml and toggling the email field, then restarting:

docker-compose restart traefik

If you ever need to delete a certificate manually:

docker exec traefik sh -c 'rm /acme/acme.json && touch /acme/acme.json && chmod 600 /acme/acme.json'
docker-compose restart traefik

11. Security Hardening Tips

Area Recommendation
Firewall Use ufw to allow only 443 (and 80 for ACME) from the internet.
Mosquitto Enable TLS on the broker itself if you want end‑to‑end encryption.
Traefik Disable the HTTP entry point if you never need it.
Passwords Store them in a vault or use mosquitto_passwd with a strong hash.
Updates Run apt update && apt upgrade regularly; use unattended-upgrades for security patches.

12. Common Pitfalls & Troubleshooting

Symptom Likely Cause Fix
Traefik shows “no routers” Labels missing or wrong Verify labels in docker-compose.yml
ACME challenge fails with 403 Forbidden Wrong domain or DNS propagation Wait for DNS to propagate; double‑check A record
Mosquitto logs “Connection refused” Container not started docker ps to confirm; check logs
Client cannot connect over TLS Wrong port (1883 vs 8883) Use -p 8883 and ensure Traefik forwards to 1883
Traefik logs “Permission denied” on acme.json File permissions chmod 600 and correct ownership

13. Scaling Up

If you want multiple brokers (e.g., one per home network), just duplicate the mosquitto service with different names and ports, and add corresponding Traefik routers.
Alternatively, use Mosquitto‑Cluster or EMQX for a truly distributed broker.


14. Clean Up

When you’re done experimenting:

docker-compose down
rm -rf ~/mqtt-lab

15. Takeaways

  • Traefik + ACME gives you a TLS‑enabled MQTT broker without manual cert handling.
  • Docker Compose keeps the configuration declarative and repeatable.
  • The same pattern works for any service you want to expose securely.

Happy hacking, and enjoy your new MQTT home‑lab!

See also