简体   繁体   中英

Docker host multiple containers with different ip address but on same port

I have three tomcat containers running on different bridge networks with different subnet and gateway For example:

container1 172.16.0.1 bridge1
container2 192.168.0.1 bridge2
container3 192.168.10.1 bridge3

These containers are running on different ports like 8081, 8082, 8083

Is there any way to run all three containers in same 8081? If it is possible, how can I do it in docker.

You need to set-up a reverse proxy . As the name suggests, this is a proxy that works in an opposite way from the standard proxy. While standard proxy gets requests from internal network and serves them from external networks (internet), the reverse proxy gets requests from external network and serves them by fetching information from internal network.

There are multiple applications that can serve as a reverse proxy, but the most used are:

Majority of the reveres proxies can run as another container on your docker. Some of this tools are easy to start since there is ample amount of tutorials.

The reverse proxy is more than just exposing single port and forwarding traffic to back-end ports. The reverse proxy can manage and distribute the load (load balancing), can change the URI that is arriving from the client to a URI that the back-end understands (URL rewriting), can change the response form the back-end (content rewriting), etc.

Reverse HTTP/HTTP traffic

What you need to do to set a reverse proxy, assuming you have HTTP services, in your example is foloowing:

  1. Decide which tool to use. As a beginner, I suggest NginX
  2. Create a configuration file for the proxy which will take the requests from the port 80 and distribute to ports 8081, 8082, 8083. Since the containers are on different network, you will need to decide if you want to forward the traffic to their IP addresses (which I don't recommend since IP can change), or to publish the ports on the host and use the host IP. Another alternative is to run all of them on the same network.
  3. Depending on the case, you need to setup the X-Forwarding-* flags and/or URL rewriting and content rewriting.
  4. Run the container and publish the port 80 as 8080 (if you expose the containers on host, your 8081 will be already taken).

Reverse TCP/UDP traffic

If you have non-HTTP services (raw TCP or UDP services), then you can use HAProxy. Steps are same apart from the configuration step #2. The configuration is different due to non-HTTP nature of the traffic and you can find example in this SO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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