Docker-compose service listen on the specified interface

I am using docker-machine and docker-compose.

The docking machine that I use has several interfaces.

Is there a way to make services listen only on a specific machine interface without specifying the IP address of this interface in the port section?

Now I use this in the docker-compose.yml file

    ports:
      - "10.5.1.28:80:80"
      - "10.5.1.28:2222:22"
      - "10.5.1.28:443:443"

But this is not very convenient, since I have to rewrite all ips in case of a change.

Is there an easier way to do this?

+4
source share
1 answer

One of the elegant solutions is to create an external network.

For example, with two nginx using external networks (Internet and intranet):

Services Definition:

  nginx1:
    build: ./nginx
    ports:
      - "80:80"
    networks:
      - intranet    
  nginx2:
    build: ./nginx
    ports:
      - "80:80"
    networks:
      - internet

Network Definition:

networks:
    internet:
      external: true
    intranet:
      external: true

, ,

192.168.178.69 - ip 10.5.1.22

docker network create -d bridge -o \
      com.docker.network.bridge.host_binding_ipv4=192.168.178.69 internet

docker network create -d bridge -o \
      com.docker.network.bridge.host_binding_ipv4=10.5.1.22 intranet
0

All Articles