title: docker-compose: specify which interface will connect to each network (when having multiple networks)
body: <p>Inside the <code>docker-compose.yaml</code> I define 4 different networks:</p>
  
  <pre><code>networks:
    vpp-nw:
    vpp-client:
    vpp-server:
    vpp-db:
  </code></pre>
  
  <p>which use the following network addresses respectively:</p>
  
  <pre><code>172.20.0.x
  172.21.0.x
  172.22.0.x
  172.23.0.x
  </code></pre>
  
  <p>One of the containers I use, connects to all 4 networks (with the same order):</p>
  
  <pre><code># part of docker-compose.yaml
  
  services:
    my_tool:
      build: ./my_tool
      image: tgogos/my_tool
      container_name: my_tool_docker_comp
      hostname: my_tool
      privileged: true
      links:
        - my_db
      networks:
        - vpp-nw
        - vpp-client
        - vpp-server
        - vpp-db
      ports:
        - "8888:8888"
  </code></pre>
  
  <p><strong>Is there a way to define which interface will connect to each network?</strong> For example, I would like:</p>
  
  <ul>
  <li><code>eth0</code> to connect to the first (vpp-nw)</li>
  <li><code>eth1</code> to connect to the second (vpp-client)</li>
  <li><code>eth2</code> to connect to the third (vpp-server)</li>
  <li><code>eth3</code> to connect to the fourth (vpp-db)</li>
  </ul>
  
  <hr>
  
  <p>Below you can see an <code>ifconfig</code> output of this container. NICs seem to connect to each network in an arbitrary way every time I <code>docker-compose down</code> | <code>docker-compose up</code>...</p>
  
  <pre><code>eth0  Link encap:Ethernet  HWaddr 02:42:ac:15:00:03  
        inet addr:172.21.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
  
  eth1  Link encap:Ethernet  HWaddr 02:42:ac:17:00:03  
        inet addr:172.23.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
  
  eth2  Link encap:Ethernet  HWaddr 02:42:ac:14:00:02  
        inet addr:172.20.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
  
  eth3  Link encap:Ethernet  HWaddr 02:42:ac:16:00:03  
        inet addr:172.22.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
  </code></pre>
  
  <h2>Edit:</h2>
  
  <p>Further reading, github issues:</p>
  
  <ul>
  <li><a href="https://github.com/docker/compose/issues/4645" rel="noreferrer">The order in which networks are assigned to interfaces does not match the order they are listed in compose file #4645</a></li>
  <li><a href="https://github.com/moby/moby/issues/34003" rel="noreferrer">17.06 L4 ingress network not accessable #34003</a></li>
  <li><a href="https://github.com/moby/moby/issues/25181" rel="noreferrer">Control interface name in docker multiple networking #25181</a></li>
  </ul>