title: How to use the host network, and any other user-defined network together in Docker-Compose?
body: <p>I want to connect two Docker containers, defined in a Docker-Compose file to each other (<code>app</code> and <code>db</code>). And one of them (<code>app</code>) should also be connected to the <code>host</code> network.</p>
  <p>The containers should be connected to a common user-defined network (<code>appnet</code> or <code>default</code>) to use the <em>embedded DNS</em> capabilities from docker networking.</p>
  <p><code>app</code> needs also to be directly connected to the host network to receive ethernet broadcasts (network layer 2) in the physical network of the docker host.</p>
  <p>Using both directives <code>network_mode: host</code> and <code>networks</code> in compose together, results in the following error:</p>
  <p><code>ERROR: 'network_mode' and 'networks' cannot be combined</code></p>
  <p>Specifying the network name <code>host</code> in the service without defining it in networks (because it already exists), results in:</p>
  <p><code>ERROR: Service &quot;app&quot; uses an undefined network &quot;host&quot;</code></p>
  <p>Next try: define both networks explicitly and do not use the <code>network_mode: host</code> attribute at service level.</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  services:
  
    app:
      build: .
      image: app
      container_name: app
      environment:
        - MONGODB_HOST=db
      depends_on:
        - db
      networks:
        - appnet
        - hostnet
  
    db:
      image: 'mongo:latest'
      container_name: db
      networks:
        - appnet
  
  networks:
    appnet: null
    hostnet:
      external:
        name: host
  </code></pre>
  <p>The foregoing compose file produces an error:</p>
  <p><code>ERROR: for app  network-scoped alias is supported only for containers in user defined networks</code></p>
  <p><strong>How to use the <code>host</code> network, and any other user-defined network (or the default) together in Docker-Compose?</strong></p>