github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/docs/reference/commandline/network_connect.md (about)

     1  # network connect
     2  
     3  <!---MARKER_GEN_START-->
     4  Connect a container to a network
     5  
     6  ### Options
     7  
     8  | Name                | Type          | Default | Description                                |
     9  |:--------------------|:--------------|:--------|:-------------------------------------------|
    10  | [`--alias`](#alias) | `stringSlice` |         | Add network-scoped alias for the container |
    11  | `--driver-opt`      | `stringSlice` |         | driver options for the network             |
    12  | [`--ip`](#ip)       | `string`      |         | IPv4 address (e.g., `172.30.100.104`)      |
    13  | `--ip6`             | `string`      |         | IPv6 address (e.g., `2001:db8::33`)        |
    14  | [`--link`](#link)   | `list`        |         | Add link to another container              |
    15  | `--link-local-ip`   | `stringSlice` |         | Add a link-local address for the container |
    16  
    17  
    18  <!---MARKER_GEN_END-->
    19  
    20  ## Description
    21  
    22  Connects a container to a network. You can connect a container by name
    23  or by ID. Once connected, the container can communicate with other containers in
    24  the same network.
    25  
    26  ## Examples
    27  
    28  ### Connect a running container to a network
    29  
    30  ```console
    31  $ docker network connect multi-host-network container1
    32  ```
    33  
    34  ### Connect a container to a network when it starts
    35  
    36  You can also use the `docker run --network=<network-name>` option to start a
    37  container and immediately connect it to a network.
    38  
    39  ```console
    40  $ docker run -itd --network=multi-host-network busybox
    41  ```
    42  
    43  ### <a name="ip"></a> Specify the IP address a container will use on a given network (--ip)
    44  
    45  You can specify the IP address you want to be assigned to the container's interface.
    46  
    47  ```console
    48  $ docker network connect --ip 10.10.36.122 multi-host-network container2
    49  ```
    50  
    51  ### <a name="link"></a> Use the legacy `--link` option (--link)
    52  
    53  You can use `--link` option to link another container with a preferred alias.
    54  
    55  ```console
    56  $ docker network connect --link container1:c1 multi-host-network container2
    57  ```
    58  
    59  ### <a name="alias"></a> Create a network alias for a container (--alias)
    60  
    61  `--alias` option can be used to resolve the container by another name in the network
    62  being connected to.
    63  
    64  ```console
    65  $ docker network connect --alias db --alias mysql multi-host-network container2
    66  ```
    67  
    68  ### Network implications of stopping, pausing, or restarting containers
    69  
    70  You can pause, restart, and stop containers that are connected to a network.
    71  A container connects to its configured networks when it runs.
    72  
    73  If specified, the container's IP address(es) is reapplied when a stopped
    74  container is restarted. If the IP address is no longer available, the container
    75  fails to start. One way to guarantee that the IP address is available is
    76  to specify an `--ip-range` when creating the network, and choose the static IP
    77  address(es) from outside that range. This ensures that the IP address is not
    78  given to another container while this container is not on the network.
    79  
    80  ```console
    81  $ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
    82  ```
    83  
    84  ```console
    85  $ docker network connect --ip 172.20.128.2 multi-host-network container2
    86  ```
    87  
    88  To verify the container is connected, use the `docker network inspect` command.
    89  Use `docker network disconnect` to remove a container from the network.
    90  
    91  Once connected in network, containers can communicate using only another
    92  container's IP address or name. For `overlay` networks or custom plugins that
    93  support multi-host connectivity, containers connected to the same multi-host
    94  network but launched from different Engines can also communicate in this way.
    95  
    96  You can connect a container to one or more networks. The networks need not be
    97  the same type. For example, you can connect a single container bridge and overlay
    98  networks.
    99  
   100  ## Related commands
   101  
   102  * [network inspect](network_inspect.md)
   103  * [network create](network_create.md)
   104  * [network disconnect](network_disconnect.md)
   105  * [network ls](network_ls.md)
   106  * [network rm](network_rm.md)
   107  * [network prune](network_prune.md)
   108  * [Understand Docker container networks](https://docs.docker.com/network/)
   109  * [Work with networks](https://docs.docker.com/network/bridge/)