github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/docs/reference/commandline/network_connect.md (about)

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