我正在尝试在 Docker 中使用 Traefik 反向代理设置示例应用程序。
我在这个项目中使用Traefik v2.2,它与 Traefik.v1.0 有显着差异。
这是我的docker-compose.yml文件:
version: '3'
services:
traefik:
# The official v2 Traefik docker image
image: traefik:v2.2
# Enables the web UI and tells Traefik to listen to docker
command:
- --api.insecure=true
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
ports:
# The HTTP port
- "89:80"
# The Web UI (enabled by --api.insecure=true)
- "8089:8080"
volumes:
# So that Traefik can listen to the Docker events
- "/var/run/docker.sock:/var/run/docker.sock:ro"
whoami:
# A container that exposes an API to show its IP address
image: containous/whoami
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
Run Code Online (Sandbox Code Playgroud)
当我在网络浏览器上访问时,我可以访问 Traefik 的仪表板,但当我在网络浏览器上输入地址时,localhost:8089我无法访问该应用程序。我只是想知道在访问它之前是否需要更改任何内容,或者我是否需要将主机更改为 ,因为这是我想要访问应用程序的端口。whoamiwhoami.localhostwhoami.localhostlocalhost:3000
小智 4
我发现的一个问题是您将80traefik 容器的容器端口暴露给主机端口89。如果您在网络浏览器中输入whoami.localhost,您的浏览器将在该地址的主机端口上搜索应用程序80(因为localhost本机映射到 port 80),但它不会在那里找到任何内容,因为它只能在端口找到89。根据我的理解,您应该能够使用命令通过命令行访问应用程序curl -H Host:whoami.localhost http://127.0.0.1:89。不幸的是,我不确定whoami.localhost:89您的浏览器和 DNS 如何处理该 URL。
您可以docker-compose.yml这样修改文件:
version: "3"
services:
traefik:
# The official v2 Traefik docker image
image: traefik:v2.2
# Enables the web UI and tells Traefik to listen to docker
command:
- --api.insecure=true
- --providers.docker=true
ports:
# The HTTP port
- "89:80"
# The Web UI (enabled by --api.insecure=true)
- "8089:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
whoami:
# A container that exposes an API to show its IP address
image: containous/whoami
labels:
- traefik.http.routers.whoami.rule=Host(`whoami.localhost`)
Run Code Online (Sandbox Code Playgroud)
然后您可以通过输入以下内容在命令终端上访问该应用程序:
curl -H Host:whoami.localhost http://127.0.0.1:89
Run Code Online (Sandbox Code Playgroud)
注意:whoami.localhost可以是whoami.docker.localhost或app.localhost或任何你想要的。这里的问题是,您应该localhost附加到末尾,除非您要添加完全合格的域名 (FQDN)。
就这样。
我希望这有帮助
| 归档时间: |
|
| 查看次数: |
8764 次 |
| 最近记录: |