port redirect to docker containers by hostname

孤街醉人 提交于 2019-12-07 22:25:55

问题


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.

Is is actual possible to use docker for port redirection via hostname?

I use a free amazon EC2 insance.

Thanks Bo

EDIT: I want to have multiple nodes applications running on the same port but however serving a different hostname.


回答1:


As far as I'm aware docker does not have such functionality built in, nor it should. To accomplish what you're trying to do you'd probably need some sort of reverse proxy, so node.js or nginx would do. Bouncy might be a good option: https://github.com/substack/bouncy




回答2:


I used varnish as a docker container that worked as my reverse proxy

it's on the docker index

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




回答3:


There is a great docker project on GitHub called nginx-proxy by jwilder. 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. Then, all you have to do is for every new web container you create, provide a new environment variable VIRTUAL_HOST=some.domain.com.

An example:

  1. Create a new nginx-proxy container

    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. In this example, I will add the to the /etc/hosts file:

    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.

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.

Hope it helps.



来源:https://stackoverflow.com/questions/21570287/port-redirect-to-docker-containers-by-hostname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!