# exposing homelab services with traefik and cloudflare tunnel, no open ports

> How I publish self-hosted apps through one Cloudflare Tunnel that points at Traefik, with no ports open on the server or the router. Includes the compose files, the header fix, and what this setup gives up.

By Harshal Ranjhani · [HTML version](https://harshalranjhani.in/blog/exposing-homelab-services-with-traefik-and-cloudflare-tunnel)

This post shows how I put self-hosted web apps on the internet without opening any ports. A Cloudflare Tunnel carries the traffic into my Docker network, and Traefik decides which container each hostname goes to. My homelab apps reach the internet this way, including the Outline wiki I keep my notes in.

I'll cover how the pieces connect, the three compose files, the tunnel route, the header and caching problems I hit with Outline, and the things this setup does not protect you from.

## how it works

Cloudflare Tunnel works through a small daemon, `cloudflared`, that you run next to your apps. It makes outbound connections to Cloudflare. When someone opens your hostname, Cloudflare sends the request back down one of those connections. Nothing on your side listens for inbound traffic from the internet, so there is no port to forward on the router and nothing to allow in the firewall.

Before I added Traefik, I pointed every tunnel hostname straight at a container. That works, but every route points at a different container and port, and there is no single place to add things like auth or headers. So now the tunnel has one target, `http://traefik:80`, and Traefik routes by hostname using Docker labels.

![Hand-drawn diagram with example hostnames. A browser connects over HTTPS to the Cloudflare edge, which handles DNS and TLS. The tunnel runs from cloudflared inside the homelab Docker network out to Cloudflare, so the arrow points outward. cloudflared forwards plain HTTP to traefik:80, and Traefik sends app.example.com and whoami.example.com to their containers. The router has no forwarded ports.](https://assets.harshalranjhani.in/images/blog/exposing-homelab-services-with-traefik-and-cloudflare-tunnel/tunnel-traefik-request-flow.png)

Each part has one job:

- **Cloudflare**: DNS and TLS for the public hostname.
- **cloudflared**: the outbound tunnel into the Docker network.
- **Traefik**: picks a container based on the `Host` header.
- **Apps**: serve plain HTTP on the internal network and publish no ports.

## what you need

- A server running Docker and Docker Compose. Any Linux box works.
- A domain added to Cloudflare. The free plan is enough for this.
- Outbound access to Cloudflare on port `7844`. Most home networks allow this already. The [firewall page](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/configure-tunnels/tunnel-with-firewall/) lists what `cloudflared` needs to reach.
- A way to reach the server that does not depend on the tunnel. I use [SSH over Tailscale](https://harshalranjhani.in/blog/ssh-home-server-with-tailscale) for that.

## step 1: create a shared docker network

Every container that Traefik should reach joins one external network. Mine is called `homelab`:

```bash
docker network create homelab
```

Traefik, `cloudflared` and the apps all attach to it. Containers on the same user-defined network can reach each other by container name, which is why the tunnel can use `traefik:80` as its target.

## step 2: run traefik without publishing ports

This is the Traefik compose file. It has no `ports:` section, so it listens on port 80 inside the Docker network and nowhere else:

```yaml
# traefik/compose.yaml
services:
  traefik:
    image: traefik:v3.7
    container_name: traefik
    command:
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--api.dashboard=true"
      - "--accesslog=true"
      - "--log.level=INFO"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - homelab
    restart: unless-stopped

networks:
  homelab:
    external: true
```

A few notes on the flags:

- `exposedbydefault=false` means Traefik ignores a container until it has the `traefik.enable=true` label. Without it, every container on the host gets a route.
- There is only one entrypoint, `web`, on plain HTTP. TLS ends at Cloudflare, so Traefik doesn't need certificates in this setup.
- The dashboard is turned on, but no hostname points at it yet. I cover that in "what it misses".
- The Docker socket is how Traefik reads labels. The [Docker provider docs](https://doc.traefik.io/traefik/reference/install-configuration/providers/docker/) explain the security trade-off of mounting it.

`v3.7` is the current stable line as I write this. v3.8 is still in release candidates.

Start it:

```bash
cd traefik
docker compose up -d
```

## step 3: create the tunnel and run cloudflared

In the Cloudflare dashboard, go to **Networking > Tunnels** and select **Create a tunnel**. The [dashboard guide](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/get-started/create-remote-tunnel/) walks through it. Pick Docker as the environment. Cloudflare shows a `docker run` command with a token in it. Copy only the token.

I run `cloudflared` as a container on the same network, so it can resolve `traefik` by name:

```yaml
# cloudflared/compose.yaml
services:
  cloudflared:
    image: cloudflare/cloudflared:2026.11.0
    container_name: cloudflared
    command: tunnel --no-autoupdate run
    environment:
      - TUNNEL_TOKEN=${TUNNEL_TOKEN}
    networks:
      - homelab
    restart: unless-stopped

networks:
  homelab:
    external: true
```

Put the token in a `.env` file next to the compose file, and keep that file out of git:

```bash
echo "TUNNEL_TOKEN=paste-your-token-here" > cloudflared/.env
chmod 600 cloudflared/.env
cd cloudflared && docker compose up -d
```

`cloudflared` reads `TUNNEL_TOKEN` from the environment, as listed in the [run parameters](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/configure-tunnels/run-parameters/). After a few seconds the tunnel should show as **Healthy** in the dashboard. If it doesn't, `docker logs cloudflared --tail 50` usually says why.

## step 4: add the route

Open the tunnel, go to the **Routes** tab, select **Add route**, then **Published application**. Fill it in like this:

```text
Subdomain:    whoami
Domain:       example.com
Path:         (empty)
Service URL:  http://traefik:80
```

Every hostname you add gets the same service URL. The tunnel never points at an app container. Cloudflare creates the DNS record for the hostname when you save the route.

For apps where the hostname matters, such as anything that sets cookies, I also set **HTTP Host Header** to the public hostname in the route's advanced settings. That is the [`httpHostHeader`](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/configure-tunnels/origin-parameters/) origin setting. If you later add a wildcard route like `*.example.com`, keep the explicit routes above it.

## step 5: put an app behind traefik

[`traefik/whoami`](https://github.com/traefik/whoami) is a tiny server that prints the request it received, which makes it a good first app. The labels tell Traefik which hostname belongs to it and which port it listens on:

```yaml
# whoami/compose.yaml
services:
  whoami:
    image: traefik/whoami
    container_name: whoami
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=homelab"
      - "traefik.http.routers.whoami.rule=Host(`whoami.example.com`)"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami.loadbalancer.server.port=80"
    networks:
      - homelab
    restart: unless-stopped

networks:
  homelab:
    external: true
```

What each label does:

- `traefik.enable=true` opts the container in.
- `traefik.docker.network=homelab` tells Traefik which network to use when the container is on more than one. My Outline stack, for example, also has a private network for its database and Redis.
- The `rule` matches the public hostname.
- `loadbalancer.server.port` is the port inside the container. For Outline that is `3000`.

Start it with `docker compose up -d`. Traefik picks up the labels on its own, so there is nothing to restart.

## step 6: check it from the inside first

Before opening the public URL, I test Traefik directly from a throwaway container on the same network. This skips Cloudflare entirely:

```bash
docker run --rm --network homelab curlimages/curl:latest \
  -s -H "Host: whoami.example.com" http://traefik:80/
```

If that prints the whoami output, Traefik and the app are fine. If the public URL then fails, the problem is in the tunnel route or DNS, not in Docker. Splitting the check this way is how I found the Outline caching bug in the next section.

Then try the public hostname from your laptop:

```bash
curl -s https://whoami.example.com/
```

![Example terminal output with placeholder hostnames and addresses, not captured from my server. The internal curl to traefik:80 with a Host header returns the whoami response. The public curl through Cloudflare returns the same app, and its X-Forwarded-Proto header reads http instead of https.](https://assets.harshalranjhani.in/images/blog/exposing-homelab-services-with-traefik-and-cloudflare-tunnel/whoami-internal-vs-public.png)

Look at `X-Forwarded-Proto` in the public response.

## two things that broke outline

The browser talks HTTPS to Cloudflare, but Traefik receives plain HTTP from `cloudflared`. Cloudflare sends `X-Forwarded-Proto: https` along with the request, but Traefik only keeps `X-Forwarded-*` headers from sources it trusts. By default it trusts nobody, so it replaces the header and the app is told the request came in over HTTP.

Many apps don't care. Apps that build absolute URLs or set `secure` cookies can. For Outline, I set the headers explicitly with a [headers middleware](https://doc.traefik.io/traefik/reference/routing-configuration/http/middlewares/headers/) on that one router:

```yaml
labels:
  - "traefik.http.middlewares.outline-forwarded.headers.customrequestheaders.X-Forwarded-Proto=https"
  - "traefik.http.middlewares.outline-forwarded.headers.customrequestheaders.X-Forwarded-Port=443"
  - "traefik.http.routers.outline-docs.middlewares=outline-forwarded"
```

The other option is to trust `cloudflared` for the whole entrypoint with [`forwardedHeaders.trustedIPs`](https://doc.traefik.io/traefik/reference/install-configuration/entrypoints/), set to the subnet of your `homelab` network. That keeps the real client IP in `X-Forwarded-For` as well. Don't use `forwardedHeaders.insecure`. The Traefik docs say it is for testing only.

The bug I actually spent time on was caching. Every save in Outline failed with "CSRF token invalid". My zone has a cache rule that caches everything, including HTML, so Cloudflare was serving a cached copy of the Outline page without the fresh `Set-Cookie: csrfToken=...` header. The internal curl from step 6 returned the cookie and the public one didn't, which pointed at Cloudflare. A [cache rule](https://developers.cloudflare.com/cache/how-to/cache-rules/) that bypasses cache for that hostname, followed by a purge, fixed it. If your zone caches HTML, add a bypass rule for every app that has a login, and make sure no broader cache-everything rule overrides it.

## step 7: make sure nothing is listening

The whole point is that no port is open, so check it. On the host:

```bash
sudo ss -tlnp
```

You should not see anything on `0.0.0.0:80` or `0.0.0.0:443`. If you do, some compose file still has a `ports:` section. Docker writes its own firewall rules for published ports, and the [Docker docs](https://docs.docker.com/engine/network/packet-filtering-firewalls/) explain that those rules apply before UFW's. A `deny` in UFW will not close a port that Docker published.

I keep UFW enabled on the server anyway. A default that denies incoming traffic and only allows the Tailscale interface looks like this:

```bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow in on tailscale0
sudo ufw enable
```

Run the `allow in on tailscale0` line before `enable` if you are connected over Tailscale, or you will lock yourself out.

![Hand-drawn diagram with example names. The server shows no published ports and a deny-incoming firewall. Inside the homelab Docker network, cloudflared, Traefik and two app containers talk over port 80 and 3000. A separate private network holds a database and Redis that only one app can reach. The only connections that leave the box are outbound to Cloudflare on port 7844 and the Tailscale link for SSH.](https://assets.harshalranjhani.in/images/blog/exposing-homelab-services-with-traefik-and-cloudflare-tunnel/what-listens-where.png)

## what it misses

**The app is still public.** No open ports means nobody can reach your server directly. It does not mean nobody can reach your app. Anyone with the hostname gets the login page, and any bug in the app is reachable through the tunnel. For admin tools, put [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/access-controls/applications/http-apps/self-hosted-public-app/) in front, and turn on **Protect with Access** in the tunnel so `cloudflared` checks the Access token too. Cloudflare's docs recommend creating the Access application before adding the route, so the hostname is never open.

**Don't publish the Traefik dashboard without Access.** It lists every router and backend you run. If you want it, add a router with `service=api@internal` and put that hostname behind Access.

**Cloudflare sees your traffic in plain text.** TLS ends at their edge and the request is encrypted again inside the tunnel. For anything you wouldn't hand to a third party, use something private like Tailscale and skip the public hostname.

**It depends on Cloudflare and on `cloudflared`.** If either is down, every app is down at once, even though the server is fine. This is another reason to keep a separate way in.

**The Docker socket is a big permission.** Traefik only needs to read labels, but the `:ro` flag on a socket mount does not limit what can be done through the Docker API. A socket proxy that only allows read endpoints narrows this.

**Large uploads hit Cloudflare's limits.** The [max upload size](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/4xx-client-error/error-413/) is 100 MB on the Free and Pro plans. Outline sends its files to S3-compatible storage, so it isn't affected, but a file sync service behind the tunnel would be.

If you already have a few apps running with published ports, start by moving one of them: add the labels, point a tunnel route at `traefik:80`, run the internal and public curl checks, then delete its `ports:` section and confirm with `ss -tlnp` that nothing is listening.

Last updated on November 17, 2026
