github.com/sld880311/docker@v0.0.0-20200524143708-d5593973a475/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  <!-- This file is maintained within the docker/docker Github
     8       repository at https://github.com/docker/docker/. Make all
     9       pull requests against that repo. If you see this file in
    10       another repository, consider it read-only there, as it will
    11       periodically be overwritten by the definitive file. Pull
    12       requests which include edits to this file in other repositories
    13       will be rejected.
    14  -->
    15  
    16  # network connect
    17  
    18  ```markdown
    19  Usage:  docker network connect [OPTIONS] NETWORK CONTAINER
    20  
    21  Connect a container to a network
    22  
    23  Options:
    24        --alias value           Add network-scoped alias for the container (default [])
    25        --help                  Print usage
    26        --ip string             IP Address
    27        --ip6 string            IPv6 Address
    28        --link value            Add link to another container (default [])
    29        --link-local-ip value   Add a link-local address for the container (default [])
    30  ```
    31  
    32  ## Description
    33  
    34  Connects a container to a network. You can connect a container by name
    35  or by ID. Once connected, the container can communicate with other containers in
    36  the same network.
    37  
    38  ## Examples
    39  
    40  ### Connect a running container to a network
    41  
    42  ```bash
    43  $ docker network connect multi-host-network container1
    44  ```
    45  
    46  ### Connect a container to a network when it starts
    47  
    48  You can also use the `docker run --network=<network-name>` option to start a container and immediately connect it to a network.
    49  
    50  ```bash
    51  $ docker run -itd --network=multi-host-network busybox
    52  ```
    53  
    54  ### Specify the IP address a container will use on a given network
    55  
    56  You can specify the IP address you want to be assigned to the container's interface.
    57  
    58  ```bash
    59  $ docker network connect --ip 10.10.36.122 multi-host-network container2
    60  ```
    61  
    62  ### Use the legacy `--link` option
    63  
    64  You can use `--link` option to link another container with a preferred alias
    65  
    66  ```bash
    67  $ docker network connect --link container1:c1 multi-host-network container2
    68  ```
    69  
    70  ### Create a network alias for a container
    71  
    72  `--alias` option can be used to resolve the container by another name in the network
    73  being connected to.
    74  
    75  ```bash
    76  $ docker network connect --alias db --alias mysql multi-host-network container2
    77  ```
    78  
    79  ### Network implications of stopping, pausing, or restarting containers
    80  
    81  You can pause, restart, and stop containers that are connected to a network.
    82  A container connects to its configured networks when it runs.
    83  
    84  If specified, the container's IP address(es) is reapplied when a stopped
    85  container is restarted. If the IP address is no longer available, the container
    86  fails to start. One way to guarantee that the IP address is available is
    87  to specify an `--ip-range` when creating the network, and choose the static IP
    88  address(es) from outside that range. This ensures that the IP address is not
    89  given to another container while this container is not on the network.
    90  
    91  ```bash
    92  $ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
    93  ```
    94  
    95  ```bash
    96  $ docker network connect --ip 172.20.128.2 multi-host-network container2
    97  ```
    98  
    99  To verify the container is connected, use the `docker network inspect` command. Use `docker network disconnect` to remove a container from the network.
   100  
   101  Once connected in network, containers can communicate using only another
   102  container's IP address or name. For `overlay` networks or custom plugins that
   103  support multi-host connectivity, containers connected to the same multi-host
   104  network but launched from different Engines can also communicate in this way.
   105  
   106  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.
   107  
   108  ## Related commands
   109  
   110  * [network inspect](network_inspect.md)
   111  * [network create](network_create.md)
   112  * [network disconnect](network_disconnect.md)
   113  * [network ls](network_ls.md)
   114  * [network rm](network_rm.md)
   115  * [network prune](network_prune.md)
   116  * [Understand Docker container networks](https://docs.docker.com/engine/userguide/networking/)
   117  * [Work with networks](https://docs.docker.com/engine/userguide/networking/work-with-networks/)