1、建立Traefik和容器應用的鏈接網絡nginx
sudo docker network create traefik-netgit
2、下載Traefik樣本配置文件github
wget https://raw.githubusercontent.com/containous/traefik/master/traefik.sample.tomlweb
3、編輯Traefik配置文件docker
sudo cp ./traefik.sample.toml ./traefik.toml瀏覽器
sudo vi ./traefik.toml網絡
編輯traefik.toml文件內容:frontend
debug = falsedom
logLevel = "ERROR"ide
defaultEntryPoints = ["http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[retry]
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "docker.localhost"
watch = true
exposedByDefault = false
usebindportip = true
swarmMode = false
network = "traefik-net"
4、在主機上運行Traefik反向代理容器
Docker運行 Traefik 命令:
docker run -d \
--name traefik \
-p 8080:8080 \
-p 80:80 \
-v $PWD/traefik.toml:/etc/traefik/traefik.toml \
-v /var/run/docker.sock:/var/run/docker.sock \
traefik
5、運行Docker容器應用
運行 Docker 容器應用web1:
docker run -d \
--name nginx \
--label "traefik.backend=nginx" \
--label "traefik.protocol=http" \
--label "traefik.port=80" \
--label "traefik.docker.network=traefik-net" \
--label "traefik.domain=abc.com" \
--label "traefik.frontend.rule=Host:web1.abc.com" \
--label "traefik.enable=true" \
nginx
運行 Docker 容器應用web2:
docker run -d \
--name httpd \
--label "traefik.backend=httpd" \
--label "traefik.protocol=http" \
--label "traefik.port=80" \
--label "traefik.docker.network=traefik-net" \
--label "traefik.domain=abc.com" \
--label "traefik.frontend.rule=Host:web2.abc.com" \
--label "traefik.enable=true" \
httpd
6、配置客戶端 hosts 主機文件,經過瀏覽器訪問容器應用
修改 Win7 客戶端電腦上 C:\Windows\System32\drivers\etc\hosts文件:
192.168.3.168 web1.abc.com
192.168.3.168 web2.abc.com
經過客戶端電腦上的瀏覽器訪問容器應用
http://web1.abc.com
http://web2.abc.com
7、查看Traefik管理後臺
http://192.168.3.168:8080
注意事項:運行容器應用時,--label 中,等號左右不能有空格!
附圖:
00-traefik.toml配置文件
01-運行 Traefik容器
02-運行 web1 和 web2 容器應用
03-運行中的容器
04-Traefik 管理後臺
05-Traefik 管理後臺2
06-經過web1.abc.com訪問網站
07-經過web2.abc.com訪問網站
參考連接:
https://docs.traefik.io/configuration/backends/docker/#docker