将虚拟主机分配给 Docker 端口 [英] Assigning vhosts to Docker ports

查看:30
本文介绍了将虚拟主机分配给 Docker 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了通配符 DNS,以便对自定义域 (*.foo) 的所有 Web 请求都映射到 Docker 主机的 IP 地址.如果我有多个容器运行 Apache(或 Nginx)实例,每个容器都会将 Apache 端口 (80) 映射到某个外部入站端口.

I have a wildcard DNS set up so that all web requests to a custom domain (*.foo) map to the IP address of the Docker host. If I have multiple containers running Apache (or Nginx) instances, each container maps the Apache port (80) to some external inbound port.

我想做的是向 container-1.foo 发出请求,它已经通过我的自定义 DNS 服务器映射到正确的 IP 地址(Docker 主机的),但是将默认端口 80 请求代理到正确的 Docker 外部端口,以便来自指定容器的正确 Apache 实例能够基于自定义域进行响应.同样,container-2.foo 将代理到第二个容器的 apache,依此类推.

What I would like to do is make a request to container-1.foo, which is already mapped to the correct IP address (of the Docker host) via my custom DNS server, but proxy the default port 80 request to the correct Docker external port such that the correct Apache instance from the specified container is able to respond based on the custom domain. Likewise, container-2.foo would proxy to a second container's apache, and so on.

是否有一个预先构建的解决方案,我最好的选择是在 Docker 主机上运行 Nginx 代理,还是我应该编写一个具有管理 Docker 容器潜力的 node.js 代理(启动/停止/reuild通过网络),或者......?我有哪些选择可以让使用 Docker 容器更像是一个自然事件,而不是带有无关端口和容器杂耍的东西?

Is there a pre-built solution for this, is my best bet to run an Nginx proxy on the Docker host, or should I write up a node.js proxy with the potential to manage Docker containers (start/stop/reuild via the web), or...? What options do I have that would make using the Docker containers more like a natural event and not something with extraneous ports and container juggling?

推荐答案

这个答案可能有点晚了,但您需要的是一个自动反向代理.我为此使用了两种解决方案:

This answer might be a bit late, but what you need is an automatic reverse proxy. I have used two solutions for that:

  • jwilder/nginx-proxy
  • Traefik

随着时间的推移,我更喜欢使用 Traefik.主要是因为它有良好的文档记录和维护,并且具有更多功能(具有不同策略和优先级的负载平衡、健康检查、断路器、带有 ACME/Let's Encrypt 的自动 SSL 证书,......).

With time, my preference is to use Traefik. Mostly because it is well documented and maintained, and comes with more features (load balancing with different strategies and priorities, healthchecks, circuit breakers, automatic SSL certificates with ACME/Let's Encrypt, ...).

运行 Docker 容器时 JasonWilder 的 nginx-proxy Docker 映像,您可以将 nginx 服务器设置为其他容器的反向代理,而无需维护配置.

When running a Docker container Jason Wilder's nginx-proxy Docker image, you get a nginx server set up as a reverse proxy for your other containers with no config to maintain.

只需使用 VIRTUAL_HOST 环境变量 运行您的其他容器,nginx-proxy 就会发现它们的 ip:port 并为您更新 nginx 配置.

Just run your other containers with the VIRTUAL_HOST environment variable and nginx-proxy will discover their ip:port and update the nginx config for you.

假设您的 DNS 设置为 *.test.local 映射到您的 Docker 主机的 IP 地址,然后只需启动以下容器即可运行快速演示:

Let say your DNS is set up so that *.test.local maps to the IP address of your Docker host, then just start the following containers to get a quick demo running:

# start the reverse proxy
docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

# start a first container for http://tutum.test.local
docker run -d -e "VIRTUAL_HOST=tutum.test.local" tutum/hello-world

# start a second container for http://deis.test.local
docker run -d -e "VIRTUAL_HOST=deis.test.local" deis/helloworld

<小时>

使用 Traefik

当运行 Traefik 容器时,您会设置一个反向代理服务器,它将重新配置其转发规则给定 <强>docker 标签在您的容器上找到.


Using Traefik

When running a Traefik container, you get a reverse proxy server set up which will reconfigure its forwarding rules given docker labels found on your containers.

假设您的 DNS 设置为 *.test.local 映射到您的 Docker 主机的 IP 地址,然后只需启动以下容器即可运行快速演示:

Let say your DNS is set up so that *.test.local maps to the IP address of your Docker host, then just start the following containers to get a quick demo running:

# start the reverse proxy
docker run --rm -it -p 80:80 -v /var/run/docker.sock:/var/run/docker.sock traefik:1.7 --docker

# start a first container for http://tutum.test.local
docker run -d -l "traefik.frontend.rule=Host:tutum.test.local" tutum/hello-world

# start a second container for http://deis.test.local
docker run -d -l "traefik.frontend.rule=Host:deis.test.local" deis/helloworld

这篇关于将虚拟主机分配给 Docker 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆