How do you define a network in a version 2 docker-compose definition file?
<p>The <a href="https://docs.docker.com/compose/compose-file/#net" rel="noreferrer">documentation about networking is currently</a> very vague on this ― how do you accomplish a <code>docker-compose.yml</code> <em>that creates</em> a virtual network, letting the services (containers) defined by it communicate on that network?</p>

<p>Goal in this scenario being not relying on a pre-defined network, for an ensemble of containers defined for docker-compose. Rather have the network definition self-contained in the docker-compose definition file.</p>

<p>With a pre-defined network, this below would work if the application in <code>A</code> used the name <code>B</code> as the hostname for accessing the application packaged inside <code>B</code> listening on its port 9000. The <code>host:port</code> it would use for it would be <code>B:9000</code> (more specifically the uri <code>mongodb://B:9000</code> in my particular case).</p>

<pre><code>foo:
  net: my-pre-defined-network
  container_name: A
  image: foo
bar:
  net: my-pre-defined-network
  container_name: B
  image: bar
  ports:
    - "9000:9000"
</code></pre>

<p>But my point is defining a network inside the docker-compose configuration, not assuming one was a-priori defined...</p>

<h1>TL;DR</h1>

<p>A default network is automatically created. See the beginning section of <a href="https://docs.docker.com/compose/networking/" rel="noreferrer">https://docs.docker.com/compose/networking/</a> for how to address containers within this network.</p>