1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<h1>docker network connect</h1> <p><br></p> <p>Connect a container to a network</p> <h2 id="usage">Usage</h2> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect [OPTIONS] NETWORK CONTAINER
</pre></div> <p>Refer to the <a href="#options">options section</a> for an overview of available <a href="#options"><code class="language-plaintext highlighter-rouge">OPTIONS</code></a> for this command.</p> <h2 id="description">Description</h2> <p name="extended-description">Connects a container to a network. You can connect a container by name or by ID. Once connected, the container can communicate with other containers in the same network.</p> <p>For example uses of this command, refer to the <a href="#examples">examples section</a> below.</p> <h2 id="options">Options</h2> <table> <thead> <tr> <td>Name, shorthand</td> <td>Default</td> <td>Description</td> </tr> </thead> <tbody> <tr> <td><code class="language-plaintext highlighter-rouge">--alias</code></td> <td></td> <td>Add network-scoped alias for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--driver-opt</code></td> <td></td> <td>driver options for the network</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--ip</code></td> <td></td> <td>IPv4 address (e.g., 172.30.100.104)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--ip6</code></td> <td></td> <td>IPv6 address (e.g., 2001:db8::33)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--link</code></td> <td></td> <td>Add link to another container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--link-local-ip</code></td> <td></td> <td>Add a link-local address for the container</td> </tr> </tbody> </table> <h2 id="examples">Examples</h2> <h3 id="connect-a-running-container-to-a-network">Connect a running container to a network</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect multi-host-network container1
</pre></div> <h3 id="connect-a-container-to-a-network-when-it-starts">Connect a container to a network when it starts</h3> <p>You can also use the <code class="language-plaintext highlighter-rouge">docker run --network=<network-name></code> option to start a container and immediately connect it to a network.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -itd --network=multi-host-network busybox
</pre></div> <h3 id="specify-the-ip-address-a-container-will-use-on-a-given-network">Specify the IP address a container will use on a given network</h3> <p>You can specify the IP address you want to be assigned to the container’s interface.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect --ip 10.10.36.122 multi-host-network container2
</pre></div> <h3 id="use-the-legacy---link-option">Use the legacy <code class="language-plaintext highlighter-rouge">--link</code> option</h3> <p>You can use <code class="language-plaintext highlighter-rouge">--link</code> option to link another container with a preferred alias</p> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect --link container1:c1 multi-host-network container2
</pre></div> <h3 id="create-a-network-alias-for-a-container">Create a network alias for a container</h3> <p><code class="language-plaintext highlighter-rouge">--alias</code> option can be used to resolve the container by another name in the network being connected to.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect --alias db --alias mysql multi-host-network container2
</pre></div> <h3 id="network-implications-of-stopping-pausing-or-restarting-containers">Network implications of stopping, pausing, or restarting containers</h3> <p>You can pause, restart, and stop containers that are connected to a network. A container connects to its configured networks when it runs.</p> <p>If specified, the container’s IP address(es) is reapplied when a stopped container is restarted. If the IP address is no longer available, the container fails to start. One way to guarantee that the IP address is available is to specify an <code class="language-plaintext highlighter-rouge">--ip-range</code> when creating the network, and choose the static IP address(es) from outside that range. This ensures that the IP address is not given to another container while this container is not on the network.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
</pre></div> <div class="highlight"><pre class="highlight" data-language="">$ docker network connect --ip 172.20.128.2 multi-host-network container2
</pre></div> <p>To verify the container is connected, use the <code class="language-plaintext highlighter-rouge">docker network inspect</code> command. Use <code class="language-plaintext highlighter-rouge">docker network disconnect</code> to remove a container from the network.</p> <p>Once connected in network, containers can communicate using only another container’s IP address or name. For <code class="language-plaintext highlighter-rouge">overlay</code> networks or custom plugins that support multi-host connectivity, containers connected to the same multi-host network but launched from different Engines can also communicate in this way.</p> <p>You can connect a container to one or more networks. The networks need not be the same type. For example, you can connect a single container bridge and overlay networks.</p> <h2 id="parent-command">Parent command</h2> <table> <thead> <tr> <th style="text-align: left">Command</th> <th style="text-align: left">Description</th> </tr> </thead> <tbody> <tr> <td style="text-align: left"><a href="../network/index">docker network</a></td> <td style="text-align: left">Manage networks</td> </tr> </tbody> </table> <h2 id="related-commands">Related commands</h2> <table> <thead> <tr> <td>Command</td> <td>Description</td> </tr> </thead> <tbody> <tr> <td><a href="index">docker network connect</a></td> <td>Connect a container to a network</td> </tr> <tr> <td><a href="../network_create/index">docker network create</a></td> <td>Create a network</td> </tr> <tr> <td><a href="../network_disconnect/index">docker network disconnect</a></td> <td>Disconnect a container from a network</td> </tr> <tr> <td><a href="../network_inspect/index">docker network inspect</a></td> <td>Display detailed information on one or more networks</td> </tr> <tr> <td><a href="../network_ls/index">docker network ls</a></td> <td>List networks</td> </tr> <tr> <td><a href="../network_prune/index">docker network prune</a></td> <td>Remove all unused networks</td> </tr> <tr> <td><a href="../network_rm/index">docker network rm</a></td> <td>Remove one or more networks</td> </tr> </tbody> </table> <div class="_attribution">
<p class="_attribution-p">
© 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/engine/reference/commandline/network_connect/" class="_attribution-link">https://docs.docker.com/engine/reference/commandline/network_connect/</a>
</p>
</div>
|