Traefik: Installation Guide
What You'll Need
1. Update your system and install Docker
First things first, let's update everything:
sudo apt update && sudo apt upgrade -y
Install the dependencies Docker needs:
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
Add Docker's GPG key and repository:
curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] <https://download.docker.com/linux/ubuntu> $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
Now install Docker:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli [containerd.io](<http://containerd.io>)
Check if it installed correctly:
sudo docker --version
Add your user to the docker group so you don't have to sudo everything:
sudo usermod -aG docker $USER
newgrp docker
2. Install Docker Compose
Docker Compose makes managing multi-container apps way easier:
sudo curl -L "<https://github.com/docker/compose/releases/download/$>(curl -s <https://api.github.com/repos/docker/compose/releases/latest> | grep -Po '"tag_name": "\\K.*?(?=")')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Make it executable:
sudo chmod +x /usr/local/bin/docker-compose
Verify:
docker-compose --version
3. Create your Traefik directory structure
Let's keep things organized:
mkdir -p ~/traefik/config ~/traefik/letsencrypt
cd ~/traefik
4. Create a Docker network for Traefik
This is important - all your services will need to connect to this network:
docker network create traefik-network
5. Create the Traefik configuration file
This is where the magic happens:
nano config/traefik.yml
Example config:
api:
dashboard: true
insecure: true
entryPoints:
web:
address: ":80"
websecure:
address: ":443"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
network: traefik-network
certificatesResolvers:
letsencrypt:
acme:
email: basitha[@](<mailto:your-email@example.com>)<my-domain-name>
storage: /letsencrypt/acme.json
httpChallenge:
entryPoint: web
6. Create your docker-compose.yml file
nano docker-compose.yml
Example config:
services:
traefik:
image: traefik:v2.10
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
networks:
- traefik-network
ports:
- "80:80"
- "443:443"
- "8080:8080" # Traefik Dashboard port
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./config/traefik.yml:/traefik.yml:ro
- ./letsencrypt:/letsencrypt
labels:
- "traefik.enable=true"
networks:
traefik-network:
external: true
7. Set permissions for SSL certificates
This is crucial for Let's Encrypt to work:
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
8. Start Traefik
Let's fire it up:
docker-compose up -d
Check if it's running:
docker ps
You should see the traefik container running. If you want to see the logs:
docker-compose logs -f traefik
9. Check the Traefik dashboard
Open your browser and go to: http://<your-server-ip>:8080
You should see the Traefik dashboard.
Useful commands you'll need
Check status:
docker-compose ps
View logs:
docker-compose logs -f traefik
Restart Traefik:
docker-compose restart traefik
Stop everything:
docker-compose down
Update Traefik:
docker-compose pull
docker-compose up -d
What is Next
Now that Traefik is running, I need to actually migrate my services to use it. I'm planning to: