> For the complete documentation index, see [llms.txt](https://localxpose.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://localxpose.gitbook.io/docs/tutorials/use-localxpose-with-traefik.md).

# Use LocalXpose with Traefik

## Traefik

Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components (Docker, Swarm mode, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS, ...) and configures itself automatically and dynamically. ([Read more](https://traefik.io))

## Architecture

For this tutorial, we will create a small lab that has three services, our blogging platform [Ghost](https://localxpose.io/apps/ghost), file hosting service [NextCloud](https://localxpose.io/apps/nextcloud) and a demo website using [Nginx](https://localxpose.io/apps/nginx) demo container.

In front of these three services we will run [Traefik](https://localxpose.io/apps/.traefik) which will mange the routing between these services and lastly will create LocalXpose container to access Traefik from the internet.

![ ](/files/RCAtdLUEHhcSQ2BDQrcr)

## Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* [Docker](https://docs.docker.com/get-docker/) and [Docker-compose](https://docs.docker.com/compose/install/).
* [LocalXpose](https://localxpose.io/#download).

## Step 1 - Reserve wildcard custom domain

We will use a [custom wildcard domain](/docs/reservations/domain.md#wildcard-custom-domain), for this example we will use this custom domain `example.com`, so we need to reserve it:

```shell
loclx domain reserve --domain '*.example.com'
```

Then we should add a CNAME record in our DNS provider, You can read more about domain reservations [here](/docs/reservations/domain.md).

## Step 2 - Prepare docker-compose stack file

Change the following `*.example.com` with your actual reserved domain and replace the access token with yours.

```yaml
version: "3.8"
services:


  # LocalXpose service will expose traefik container to the internet
  localxpose:
    image: localxpose/localxpose:latest # read more here https://hub.docker.com/r/localxpose/localxpose
    # forward any incoming requests to traefik container
    command: tunnel -r http --reserved-domain "*.example.com" --to traefik:80
    environment:
     # Get your access token from your dashboard here https://localxpose.io/dashboard/access
     ACCESS_TOKEN: YOURS_HERE



  # Traefik service which will handle the routing between the other 
  # docker services
  traefik:
    image: traefik:v2.8
    command:
      # Enabling docker provider
      - "--providers.docker=true"
      # Do not expose containers unless explicitly told so
      - "--providers.docker.exposedbydefault=false"
      # Traefik will listen to incoming request on the port 80 (HTTP)
      - "--entrypoints.web.address=:80"
    volumes:
    - "/var/run/docker.sock:/var/run/docker.sock:ro"




  # Nginx service is a demo application
  nginx:
    image: nginxdemos/hello:0.3
    labels:
      # Explicitly tell Traefik to expose this container
      - "traefik.enable=true"
      # The domain the service will respond to
      - "traefik.http.routers.nginx.rule=Host(`nginx.example.com`)"
      # Allow request only from the predefined entry point named "web"
      - "traefik.http.routers.nginx.entrypoints=web"
      # Tell Traefik to use the port 80 to connect to `nginx` container
      - "traefik.http.services.nginx.loadbalancer.server.port=80"



  # Nextcloud is our file hosting service
  nextcloud:
    image: nextcloud:stable-apache
    labels:
      # Explicitly tell Traefik to expose this container
      - "traefik.enable=true"
      # The domain the service will respond to
      - "traefik.http.routers.nextcloud.rule=Host(`nextcloud.example.com`)"
      # Allow request only from the predefined entry point named "web"
      - "traefik.http.routers.nextcloud.entrypoints=web"
      # Tell Traefik to use the port 80 to connect to `nextcloud` container
      - "traefik.http.services.nextcloud.loadbalancer.server.port=80"




  # Ghost is our blogging platform
  ghost:
    image: ghost:5.5.0
    environment:
      url: http://blog.example.com
    labels:
      # Explicitly tell Traefik to expose this container
      - "traefik.enable=true"
      # The domain the service will respond to
      - "traefik.http.routers.ghost.rule=Host(`blog.example.com`)"
      # Allow request only from the predefined entry point named "web"
      - "traefik.http.routers.ghost.entrypoints=web"
      # Tell Traefik to use the port 2368 to connect to `ghost` container
      - "traefik.http.services.ghost.loadbalancer.server.port=2368"

```

## Step 3 - Start our stack file

```shell
docker-compose up
```

## Step 4 - Access these services from the internet

Now you can access these services from the internet like `https://nginx.example.com`, `https://blog.example.com` and `https://nextcloud.example.com`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://localxpose.gitbook.io/docs/tutorials/use-localxpose-with-traefik.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
