Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose service listen on specified interface

I use docker-machine and docker-compose.

The docker machine I use has multiple interfaces.

Is there a way to force services to listen only on a specific interface of the machine without defining the ip address of this interface in the port section?

Right now I use this in docker-compose.yml

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

But it is not very convenient as I have to rewrite all ips in case of change.

Is there any simplier way to do this?

like image 541
user1707414 Avatar asked May 22 '16 19:05

user1707414


1 Answers

One elegant solution is to create external network.

For example with two nginx using external networks (internet & 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

Then prior first docker-compose up you will have to create the external network

In this example 192.168.178.69 is the ip on the internet side and 10.5.1.22 on the intranet side

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
like image 178
user1707414 Avatar answered Sep 20 '22 13:09

user1707414