遵循本教程
version: '3.8'
services:
reverse-proxy:
container_name: traefik
image: traefik:v2.5
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
whoami:
container_name: whoami
image: "traefik/whoami"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`whoami.vws.dev`)"
- "traefik.http.routers.whoami.entrypoints=web"
当我在浏览器上尝试时,我无法从获得响应
- http://vws.dev
- http://vws.dev:80
- http://whoami.vws.dev
您在docker-compose.yml
文件中配置的任何内容都无法使您的浏览器"知道";如何将名称vws.dev
映射到您的容器。如果您打开撰写堆栈,您可以通过显式设置Host
标头来验证它是否使用curl
工作,如下所示:
curl -H 'Host: whoami.vws.dev` localhost:80
在我的系统上打印出类似于:
Hostname: 65df3ef3b7d2
IP: 127.0.0.1
IP: 172.19.0.3
RemoteAddr: 172.19.0.2:43904
GET / HTTP/1.1
Host: whoami.vws.dev
User-Agent: curl/7.76.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.19.0.1
X-Forwarded-Host: whoami.vws.dev
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 2b2a7ec697d6
X-Real-Ip: 172.19.0.1
如果你想在浏览器中访问http://whoami.vws.dev
,你必须以某种方式将该名称映射到容器的ip地址。在类似unix的系统上,通常可以通过编辑/etc/hosts
文件来完成此操作;例如,我可以通过将whoami.vws.dev
附加到名称列表中来修改127.0.0.1
的条目:
127.0.0.1 localhost ... whoami.vws.dev
有了这个改变,我可以运行:
$ curl whoami.vws.dev
Hostname: 65df3ef3b7d2
IP: 127.0.0.1
IP: 172.19.0.3
RemoteAddr: 172.19.0.1:43748
GET / HTTP/1.1
Host: whoami.vws.dev
User-Agent: curl/7.76.1
Accept: */*
对于";真实的";部署时,您通常会添加一个DNS条目,将您所需的主机名映射到docker主机的地址。