summaryrefslogtreecommitdiff
path: root/devdocs/docker/compose%2Fnetworking%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/docker/compose%2Fnetworking%2Findex.html
new repository
Diffstat (limited to 'devdocs/docker/compose%2Fnetworking%2Findex.html')
-rw-r--r--devdocs/docker/compose%2Fnetworking%2Findex.html82
1 files changed, 82 insertions, 0 deletions
diff --git a/devdocs/docker/compose%2Fnetworking%2Findex.html b/devdocs/docker/compose%2Fnetworking%2Findex.html
new file mode 100644
index 00000000..04f9ffc5
--- /dev/null
+++ b/devdocs/docker/compose%2Fnetworking%2Findex.html
@@ -0,0 +1,82 @@
+<h1>Networking in Compose</h1>
+
+<blockquote> <p>This page applies to Compose file formats <a href="../compose-file/compose-file-v2/index">version 2</a> and <a href="../compose-file/index">higher</a>. Networking features are not supported for Compose file version 1 (deprecated).</p> </blockquote> <p>By default Compose sets up a single <a href="../../engine/reference/commandline/network_create/index">network</a> for your app. Each container for a service joins the default network and is both <em>reachable</em> by other containers on that network, and <em>discoverable</em> by them at a hostname identical to the container name.</p> <blockquote> <p><strong>Note</strong></p> <p>Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the <a href="../reference/index"><code class="language-plaintext highlighter-rouge">--project-name</code> flag</a> or the <a href="../reference/envvars/index#compose_project_name"><code class="language-plaintext highlighter-rouge">COMPOSE_PROJECT_NAME</code> environment variable</a>.</p> </blockquote> <p>For example, suppose your app is in a directory called <code class="language-plaintext highlighter-rouge">myapp</code>, and your <code class="language-plaintext highlighter-rouge">docker-compose.yml</code> looks like this:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
+services:
+ web:
+ build: .
+ ports:
+ - "8000:8000"
+ db:
+ image: postgres
+ ports:
+ - "8001:5432"
+</pre></div> <p>When you run <code class="language-plaintext highlighter-rouge">docker-compose up</code>, the following happens:</p> <ol> <li>A network called <code class="language-plaintext highlighter-rouge">myapp_default</code> is created.</li> <li>A container is created using <code class="language-plaintext highlighter-rouge">web</code>’s configuration. It joins the network <code class="language-plaintext highlighter-rouge">myapp_default</code> under the name <code class="language-plaintext highlighter-rouge">web</code>.</li> <li>A container is created using <code class="language-plaintext highlighter-rouge">db</code>’s configuration. It joins the network <code class="language-plaintext highlighter-rouge">myapp_default</code> under the name <code class="language-plaintext highlighter-rouge">db</code>.</li> </ol> <blockquote> <p><strong>In v2.1+, overlay networks are always <code class="language-plaintext highlighter-rouge">attachable</code></strong></p> <p>Starting in Compose file format 2.1, overlay networks are always created as <code class="language-plaintext highlighter-rouge">attachable</code>, and this is not configurable. This means that standalone containers can connect to overlay networks.</p> <p>In Compose file format 3.x, you can optionally set the <code class="language-plaintext highlighter-rouge">attachable</code> property to <code class="language-plaintext highlighter-rouge">false</code>.</p> </blockquote> <p>Each container can now look up the hostname <code class="language-plaintext highlighter-rouge">web</code> or <code class="language-plaintext highlighter-rouge">db</code> and get back the appropriate container’s IP address. For example, <code class="language-plaintext highlighter-rouge">web</code>’s application code could connect to the URL <code class="language-plaintext highlighter-rouge">postgres://db:5432</code> and start using the Postgres database.</p> <p>It is important to note the distinction between <code class="language-plaintext highlighter-rouge">HOST_PORT</code> and <code class="language-plaintext highlighter-rouge">CONTAINER_PORT</code>. In the above example, for <code class="language-plaintext highlighter-rouge">db</code>, the <code class="language-plaintext highlighter-rouge">HOST_PORT</code> is <code class="language-plaintext highlighter-rouge">8001</code> and the container port is <code class="language-plaintext highlighter-rouge">5432</code> (postgres default). Networked service-to-service communication uses the <code class="language-plaintext highlighter-rouge">CONTAINER_PORT</code>. When <code class="language-plaintext highlighter-rouge">HOST_PORT</code> is defined, the service is accessible outside the swarm as well.</p> <p>Within the <code class="language-plaintext highlighter-rouge">web</code> container, your connection string to <code class="language-plaintext highlighter-rouge">db</code> would look like <code class="language-plaintext highlighter-rouge">postgres://db:5432</code>, and from the host machine, the connection string would look like <code class="language-plaintext highlighter-rouge">postgres://{DOCKER_IP}:8001</code>.</p> <h2 id="update-containers">Update containers</h2> <p>If you make a configuration change to a service and run <code class="language-plaintext highlighter-rouge">docker-compose up</code> to update it, the old container is removed and the new one joins the network under a different IP address but the same name. Running containers can look up that name and connect to the new address, but the old address stops working.</p> <p>If any containers have connections open to the old container, they are closed. It is a container’s responsibility to detect this condition, look up the name again and reconnect.</p> <h2 id="links">Links</h2> <p>Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name. In the following example, <code class="language-plaintext highlighter-rouge">db</code> is reachable from <code class="language-plaintext highlighter-rouge">web</code> at the hostnames <code class="language-plaintext highlighter-rouge">db</code> and <code class="language-plaintext highlighter-rouge">database</code>:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
+services:
+
+ web:
+ build: .
+ links:
+ - "db:database"
+ db:
+ image: postgres
+</pre></div> <p>See the <a href="../compose-file/compose-file-v2/index#links">links reference</a> for more information.</p> <h2 id="multi-host-networking">Multi-host networking</h2> <p>When deploying a Compose application on a Docker Engine with <a href="../../engine/swarm/index">Swarm mode enabled</a>, you can make use of the built-in <code class="language-plaintext highlighter-rouge">overlay</code> driver to enable multi-host communication.</p> <p>Consult the <a href="../../engine/swarm/index">Swarm mode section</a>, to see how to set up a Swarm cluster, and the <a href="https://docs.docker.com/network/network-tutorial-overlay/">Getting started with multi-host networking</a> to learn about multi-host overlay networks.</p> <h2 id="specify-custom-networks">Specify custom networks</h2> <p>Instead of just using the default app network, you can specify your own networks with the top-level <code class="language-plaintext highlighter-rouge">networks</code> key. This lets you create more complex topologies and specify <a href="../../engine/extend/plugins_network/index">custom network drivers</a> and options. You can also use it to connect services to externally-created networks which aren’t managed by Compose.</p> <p>Each service can specify what networks to connect to with the <em>service-level</em> <code class="language-plaintext highlighter-rouge">networks</code> key, which is a list of names referencing entries under the <em>top-level</em> <code class="language-plaintext highlighter-rouge">networks</code> key.</p> <p>Here’s an example Compose file defining two custom networks. The <code class="language-plaintext highlighter-rouge">proxy</code> service is isolated from the <code class="language-plaintext highlighter-rouge">db</code> service, because they do not share a network in common - only <code class="language-plaintext highlighter-rouge">app</code> can talk to both.</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
+
+services:
+ proxy:
+ build: ./proxy
+ networks:
+ - frontend
+ app:
+ build: ./app
+ networks:
+ - frontend
+ - backend
+ db:
+ image: postgres
+ networks:
+ - backend
+
+networks:
+ frontend:
+ # Use a custom driver
+ driver: custom-driver-1
+ backend:
+ # Use a custom driver which takes special options
+ driver: custom-driver-2
+ driver_opts:
+ foo: "1"
+ bar: "2"
+</pre></div> <p>Networks can be configured with static IP addresses by setting the <a href="../compose-file/compose-file-v2/index#ipv4_address-ipv6_address">ipv4_address and/or ipv6_address</a> for each attached network.</p> <p>Networks can also be given a <a href="../compose-file/compose-file-v3/index#network-configuration-reference">custom name</a> (since version 3.5):</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
+services:
+ # ...
+networks:
+ frontend:
+ name: custom_frontend
+ driver: custom-driver-1
+</pre></div> <p>For full details of the network configuration options available, see the following references:</p> <ul> <li><a href="../compose-file/compose-file-v2/index#network-configuration-reference">Top-level <code class="language-plaintext highlighter-rouge">networks</code> key</a></li> <li><a href="../compose-file/compose-file-v2/index#networks">Service-level <code class="language-plaintext highlighter-rouge">networks</code> key</a></li> </ul> <h2 id="configure-the-default-network">Configure the default network</h2> <p>Instead of (or as well as) specifying your own networks, you can also change the settings of the app-wide default network by defining an entry under <code class="language-plaintext highlighter-rouge">networks</code> named <code class="language-plaintext highlighter-rouge">default</code>:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
+services:
+ web:
+ build: .
+ ports:
+ - "8000:8000"
+ db:
+ image: postgres
+
+networks:
+ default:
+ # Use a custom driver
+ driver: custom-driver-1
+</pre></div> <h2 id="use-a-pre-existing-network">Use a pre-existing network</h2> <p>If you want your containers to join a pre-existing network, use the <a href="../compose-file/compose-file-v2/index#network-configuration-reference"><code class="language-plaintext highlighter-rouge">external</code> option</a>:</p> <div class="highlight"><pre class="highlight" data-language="">services:
+ # ...
+networks:
+ default:
+ name: my-pre-existing-network
+ external: true
+</pre></div> <p>Instead of attempting to create a network called <code class="language-plaintext highlighter-rouge">[projectname]_default</code>, Compose looks for a network called <code class="language-plaintext highlighter-rouge">my-pre-existing-network</code> and connect your app’s containers to it.</p>
+<p><a href="https://docs.docker.com/search/?q=documentation">documentation</a>, <a href="https://docs.docker.com/search/?q=docs">docs</a>, <a href="https://docs.docker.com/search/?q=docker">docker</a>, <a href="https://docs.docker.com/search/?q=compose">compose</a>, <a href="https://docs.docker.com/search/?q=orchestration">orchestration</a>, <a href="https://docs.docker.com/search/?q=containers">containers</a>, <a href="https://docs.docker.com/search/?q=networking">networking</a></p>
+<div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2019 Docker, Inc.<br>Licensed under the Apache License, Version 2.0.<br>Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries.<br>Docker, Inc. and other parties may also have trademark rights in other terms used herein.<br>
+ <a href="https://docs.docker.com/compose/networking/" class="_attribution-link">https://docs.docker.com/compose/networking/</a>
+ </p>
+</div>