Installing Traefik on Linux
3 minutes read •
Docker Setup
Firewall limitations:
Warning
-
Before you install Docker, make sure you consider the following security implications and firewall incompatibilities.
-
If you use ufw or firewalld to manage firewall settings, be aware that when you expose container ports using Docker, these ports bypass your firewall rules. For more information, refer to Docker and ufw.
-
Docker is only compatible with
iptables-nftandiptables-legacy. Firewall rules created withnftare not supported on a system with Docker installed. Make sure that any firewall rulesets you use are created withiptablesorip6tables, and that you add them to theDOCKER-USERchain, see Packet filtering and firewalls.
Intallation (Debian)
# Add Docker's official GPG key:
# Add the repository to Apt sources:
| \
# Update apt database
# Install docker and other goodies
Verify that the installation is successful by running the hello-world image:
Traefik
- Create a directory for traefik.
- Create a new bridge network to have docker’s dns resolution. (Here named
docbridge)
- Create a
docker-compose.ymlfile with the following content:
Put the following config:
---
services:
traefik:
image: "traefik:v3.2.1"
container_name: "traefik"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./config/traefik.yml:/etc/traefik/traefik.yml:ro"
- "./config/conf:/etc/traefik/conf:ro"
networks:
- docbridge
restart: unless-stopped
networks:
docbridge:
external: true
- Create
traefik.ymlconfig file in theconfigdirectory for traefik docker container.
Put the following config:
global:
checkNewVersion: false
sendAnonymousUsage: false
log:
level: DEBUG
api:
dashboard: true
insecure: true
entryPoints:
web:
address: :80
websecure:
address: :443
providers:
file:
directory: "/etc/traefik/conf"
watch: true
- Set Traefik Router rules
Put the following config
---
http:
routers:
nginx-http:
rule: HostRegexp(`nginx`)
service: service-1
entryPoints:
- web
services:
service-1:
loadBalancer:
servers:
- url: "http://nginx"
Note: For “url” we have provided the nginx container name which is “nginx”. That is because nginx in this set-up is running as a docker container. And the Docker bridge networks have internal DNS resolution built-in by Docker. That’s why it is possible to point to nginx by simply using the container name, “nginx” but we have to provide the protocol here which is “http://”. For the “url” We can also use “domain”, “IP” and “IP:Port”. For example, http://127.0.0.1:8080 or http://localhost:8000 or simply https://example.com and so on.
Setup demo docker containers (Nginx)
- Create a directory for nginx’s docker container and a
docker-compose.ymlfile.
Put the following config inside the docker-compose.yml.
---
services:
nginx:
image: "nginx:latest"
container_name: "nginx_demo"
restart: unless-stopped
networks:
- docbridge
networks:
docbridge:
external: true
Deploy docker containers
- We can deploy a docker container by going to the root directory of a container’s docker files (where
docker-compose.ymllives). We can run the container in detach mode by providing the-dflag. Otherwise, it will run in foreground and we can see the logs live.
- Deploy the Traefik container and put it to detach mode:
- If a docker container is running on foreground (without
-d). Then we can put the containerdownwith:
||
- Or, if a container is running in the background (with
-d). Then we can put the containerdownby simply pressingCTRL+C.
Done!