简体   繁体   English

端口通过主机名重定向到 docker 容器

[英]port redirect to docker containers by hostname

I want to setup serve multiple sites from one server:我想从一台服务器设置为多个站点提供服务:

1. http://www.example.org => node.js-www (running on port (50000)
2. http://files.example.org => node.js-files (running on port 50001)

Until now I only found out to have docker doing port redirect when using static ips.直到现在我才发现在使用静态 ips 时让 docker 做端口重定向。

Is is actual possible to use docker for port redirection via hostname?实际上是否可以使用 docker 通过主机名进行端口重定向?

I use a free amazon EC2 insance.我使用免费的亚马逊 EC2 实例。

Thanks Bo谢谢博

EDIT : I want to have multiple nodes applications running on the same port but however serving a different hostname.编辑:我想让多个节点应用程序在同一个端口上运行,但服务于不同的主机名。

As far as I'm aware docker does not have such functionality built in, nor it should.据我所知 docker 没有内置这样的功能,也不应该。 To accomplish what you're trying to do you'd probably need some sort of reverse proxy, so node.js or nginx would do.要完成您想要做的事情,您可能需要某种反向代理,因此 node.js 或 nginx 可以。 Bouncy might be a good option: https://github.com/substack/bouncy Bouncy 可能是一个不错的选择: https : //github.com/substack/bouncy

There is a great docker project on GitHub called nginx-proxy by jwilder. GitHub 上有一个很棒的 docker 项目,jwilder 称为nginx-proxy This allows you to create a docker container that is doing a reverse-proxy by mapping only his port 80/443 to the host, instead of other containers.这允许您通过仅将其端口 80/443 映射到主机而不是其他容器来创建一个执行反向代理的 docker 容器。 Then, all you have to do is for every new web container you create, provide a new environment variable VIRTUAL_HOST=some.domain.com .然后,您所要做的就是为您创建的每个新 Web 容器提供一个新的环境变量VIRTUAL_HOST=some.domain.com

An example:一个例子:

  1. Create a new nginx-proxy container创建一个新的 nginx-proxy 容器

    docker run -d -p 80:80 --net shared_hosting -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
  2. Create a container for each website.为每个网站创建一个容器。 For example:例如:

     docker run -d -p 80 --net shared_hosting -e VIRTUAL_HOST=hello1.domain.com tutum/hello-world docker run -d -p 80 --net shared_hosting -e VIRTUAL_HOST=drupal.domain.com drupal
  3. You need to make sure that the hosts you own, configured in DNS to point to the server that runs the docker container.您需要确保您拥有的主机在 DNS 中配置为指向运行 docker 容器的服务器。 In this example, I will add the to the /etc/hosts file:在本例中,我将添加到/etc/hosts文件中:

     echo "127.0.0.1 hello1.domain.com drupal.domain.com" >> /etc/hosts
  4. Navigate to http://hello1.domain.com and then to http://drupal.domain.com , and see that they both use port 80 but give you a different pages.导航到http://hello1.domain.com ,然后导航到http://drupal.domain.com ,看到它们都使用端口 80 但为您提供不同的页面。

An important note about this service.关于此服务的重要说明。 As you noticed, I have added --net argument, this is because all containers you want to be a part of a shared hosting (proxy and websites) must be on the same virtual network (this can be defined by the argument --net or --network to the docker run command), especially when you use docker-compose to create dockers, because docker-compose creates its own virtual network, thus makes one container not reachable by another, so make sure the network is explicitly defined in the docker-compose.yml file.正如您所注意到的,我添加了--net参数,这是因为您希望成为共享主机(代理和网站)一部分的所有容器都必须位于同一个虚拟网络上(这可以由参数--net定义)或--network--network docker run命令),尤其是当您使用docker-compose创建 docker 时,因为docker-compose创建了自己的虚拟网络,从而使另一个容器无法访问,因此请确保网络明确定义在docker-compose.yml文件。

Hope it helps.希望能帮助到你。

I used varnish as a docker container that worked as my reverse proxy我使用 varnish 作为作为我的反向代理的 docker 容器

it's on the docker index它在 docker 索引上

https://index.docker.io/u/sysdia/docker-varnish/ https://index.docker.io/u/sysdia/docker-varnish/

I know this is an old question, but ran across it and wanted to point out that there are much cleaner ways to do what was requested.我知道这是一个老问题,但遇到了它并想指出有更清洁的方法来执行请求。 Since you are using AWS, you can have each of your two hostnames pointing at their own load balancer (ELB) in Route53.由于您使用的是 AWS,因此您可以让两个主机名中的每一个都指向 Route53 中各自的负载均衡器 (ELB)。 You could then deploy your container into ECS, for example, listening on both ports.然后,您可以将容器部署到 ECS,例如,侦听两个端口。 Each of those load balancers can redirect traffic to the appropriate listening port.这些负载均衡器中的每一个都可以将流量重定向到适当的侦听端口。 Now you have accomplished what you want, and if your traffic becomes too heavy or imbalanced, you can easily split the tasks into two different ECS clusters so they can scale independently.现在你已经完成了你想要的,如果你的流量变得太重或不平衡,你可以轻松地将任务拆分为两个不同的 ECS 集群,以便它们可以独立扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM