2015-09-29 01:57:03 +00:00
<!-- [metadata]>
+++
title = "network connect"
description = "The network connect command description and usage"
2015-09-30 20:11:36 +00:00
keywords = ["network, connect, user-defined"]
2015-09-29 01:57:03 +00:00
[menu.main]
parent = "smn_cli"
+++
<![end-metadata]-->
# network connect
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
Connects a container to a network
2016-01-08 13:45:56 +00:00
--alias=[] Add network-scoped alias for the container
2015-12-23 14:37:06 +00:00
--help Print usage
2016-01-12 02:03:40 +00:00
--ip IPv4 Address
2016-01-11 10:34:17 +00:00
--ip6 IPv6 Address
--link=[] Add a link to another container
2015-09-29 01:57:03 +00:00
2016-01-12 01:13:39 +00:00
Connects a container to a network. You can connect a container by name
2015-10-18 23:47:32 +00:00
or by ID. Once connected, the container can communicate with other containers in
the same network.
2015-09-29 01:57:03 +00:00
2015-10-18 23:47:32 +00:00
```bash
$ docker network connect multi-host-network container1
2015-09-29 01:57:03 +00:00
```
2015-10-18 23:47:32 +00:00
You can also use the `docker run --net=<network-name>` option to start a container and immediately connect it to a network.
```bash
$ docker run -itd --net=multi-host-network busybox
2015-09-29 01:57:03 +00:00
```
2016-01-08 00:18:34 +00:00
You can specify the IP address you want to be assigned to the container's interface.
```bash
2016-01-11 10:34:17 +00:00
$ docker network connect --ip 10.10.36.122 multi-host-network container2
```
You can use `--link` option to link another container with a prefered alias
```bash
$ docker network connect --link container1:c1 multi-host-network container2
2016-01-08 00:18:34 +00:00
```
2016-01-08 13:45:56 +00:00
`--alias` option can be used to resolve the container by another name in the network
being connected to.
```bash
$ docker network connect --alias db --alias mysql multi-host-network container2
```
2015-10-18 23:47:32 +00:00
You can pause, restart, and stop containers that are connected to a network.
2016-01-12 02:03:40 +00:00
Paused containers remain connected and can be revealed by a `network inspect` .
When the container is stopped, it does not appear on the network until you restart
it. If specified, the container's IP address(es) will be reapplied (if still available)
when a stopped container rejoins the network. One way to guarantee that the container
will be assigned the same IP addresses when it rejoins the network after a stop
or a disconnect, is to specify the `--ip-range` when creating the network, and choose
the static IP address(es) from outside the range. This will ensure that the IP address
will not be given to other dynamic containers while this container is not on the network.
```bash
$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
```
```bash
$ docker network connect --ip 172.20.128.2 multi-host-network container2
```
2015-10-18 23:47:32 +00:00
To verify the container is connected, use the `docker network inspect` command. Use `docker network disconnect` to remove a container from the network.
Once connected in network, containers can communicate using only another
container's IP address or name. For `overlay` 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.
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.
## Related information
2015-09-29 01:57:03 +00:00
2015-10-18 23:47:32 +00:00
* [network inspect ](network_inspect.md )
* [network create ](network_create.md )
* [network disconnect ](network_disconnect.md )
* [network ls ](network_ls.md )
* [network rm ](network_rm.md )
* [Understand Docker container networks ](../../userguide/networking/dockernetworks.md )
2016-01-11 10:34:17 +00:00
* [Work with networks ](../../userguide/networking/work-with-networks.md )