Score:0

How to correctly configure a host with two network interfaces so that the docker container can provide services normally?

us flag

My server has two network interfaces, so two IPs, one Internet IP, and one private address.

I added the following script to the /etc/iproute2/rt_tables:

252 enp0s3
251 enp0s8

And added the following script to the /etc/rc.local:

ip route add default via 172.17.171.151 dev enp0s3 table enp0s3
ip rule add from 172.17.171.151 table enp0s3

ip route add default via 172.17.168.121 dev enp0s8 table enp0s8
ip rule add from 172.17.168.121 table enp0s8

Now, everything worked fine.

I can ping these two ips, so icmp has been working normally. I can also SSH to the server through any IP, so port 22 also works well.

However, when I use the following docker-compose to start an nginx, I found that only the port 80 of any one of the 2 IPs can be accessed by the client browser, and the port 80 of the other IP is unresponsive.

version: "3"
services:
  nginx:
    restart: always
    container_name: nginx
    image: nginx
    ports:
     - 0.0.0.0:80:80
    volumes:
     - ./log:/var/log/nginx

What caused this? How can I get out of this situation?

Note: The above configuration file is a test environment I built with a virtual machine, so the IP addresses are all private addresses.

Steps to reproduce:

  1. Use Oracle VM VirtualBox to start a ubuntu 16.04 with two bridge network interfaces.
  2. Log in to the virtual machine.
  3. Refer to the above script to modify /etc/iproute2/rt_tables and /etc/rc.local.
  4. Install docker and docker-compose.
  5. Use the docker-compose above to start an Nginx.
  6. No matter which network interface is opened, the host can access port 80 of the corresponding IP address of the virtual machine.
  7. If two network interfaces are opened at the same time, only port 80 of one of the network interfaces can be opened, and the other has no response.

Supplementary information: If I start Nginx directly on the virtual machine, port 80 of the two IPs can be accessed by the host machine.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.