This repo demonstrates how to use a single Traefik Docker container as a reverse proxy for multiple projects, each with different domains. Instead of manually installing and configuring a reverse proxy for a server, you can deploy this pre-configured Traefik container and easily add as many projects with as many domains as you need.
root/
├── traefik/
│ ├── docker-compose.yml
├── project-1/
│ ├── docker-compose.yml
├── project-2/
│ ├── docker-compose.yml
├── README.md
└── ... (additional projects)
Before getting started, ensure you have the following installed on your system:
- Docker - How to install Docker (version 20.10.0 or higher)
- Docker Compose - How to install Docker Compose (version 1.27.0 or higher)
- Git - How to Install Git on Ubuntu
- A registered domain name pointed to your server's IP address - What are DNS Records? - What is a DNS A Record?
- Open ports
80
and443
on your server - How to Open a Port on a Linux Server Using UFW?
Follow these steps to set up the Traefik reverse proxy and deploy multiple projects.
git clone https://github.com/sesto-dev/one-traefik-multiple-projects.git
Traefik and the projects need to communicate over a shared Docker network.
docker network create traefik-public
Set up the necessary environment variables for Traefik and each project.
cd one-traefik-multiple-projects/traefik
Create a .env
file inside the traefik
directory:
cp .env.example .env
nano .env
Edit the .env
file to set your domain and other necessary variables.
USERNAME=your_admin_username
PASSWORD=your_admin_password
DOMAIN=yourdomain.com
ACME_EMAIL=youremail@domain.com
Note: To generate and set the hashed password in one command for Traefik's HTTP Basic Auth, you can use the following command:
export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
Then spin up your Traefik container:
docker compose up -d
For each project, create a .env
file based on the provided examples and enter the DOMAIN for that project.
cd ../project-1
cp .env.example .env
nano .env`
DOMAIN=project1.yourdomain.com
Then spin up the project container:
docker compose up -d
Repeat for project-2.
Open your browser and navigate to the domains you've configured. You should see the projects running correctly. You can also visit Traefik's dashboard at https://traefik.yourdomain.com
.