2016-10-14 18:30:36 -04:00
|
|
|
---
|
|
|
|
title: "network connect"
|
|
|
|
description: "The network connect command description and usage"
|
2016-11-03 18:48:30 -04:00
|
|
|
keywords: "network, connect, user-defined"
|
2016-10-14 18:30:36 -04:00
|
|
|
---
|
2015-09-28 21:57:03 -04:00
|
|
|
|
|
|
|
# network connect
|
|
|
|
|
2016-07-07 14:43:18 -04:00
|
|
|
```markdown
|
|
|
|
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
|
|
|
|
|
|
|
|
Connect a container to a network
|
|
|
|
|
|
|
|
Options:
|
|
|
|
--alias value Add network-scoped alias for the container (default [])
|
|
|
|
--help Print usage
|
2016-12-28 06:34:32 -05:00
|
|
|
--ip string IPv4 address (e.g., 172.30.100.104)
|
|
|
|
--ip6 string IPv6 address (e.g., 2001:db8::33)
|
2016-07-07 14:43:18 -04:00
|
|
|
--link value Add link to another container (default [])
|
|
|
|
--link-local-ip value Add a link-local address for the container (default [])
|
|
|
|
```
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Description
|
|
|
|
|
2016-01-11 20:13:39 -05:00
|
|
|
Connects a container to a network. You can connect a container by name
|
2015-10-18 19:47:32 -04:00
|
|
|
or by ID. Once connected, the container can communicate with other containers in
|
|
|
|
the same network.
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
### Connect a running container to a network
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2015-10-18 19:47:32 -04:00
|
|
|
$ docker network connect multi-host-network container1
|
2015-09-28 21:57:03 -04:00
|
|
|
```
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
### Connect a container to a network when it starts
|
|
|
|
|
2022-03-30 08:33:44 -04:00
|
|
|
You can also use the `docker run --network=<network-name>` option to start a
|
|
|
|
container and immediately connect it to a network.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-06-06 19:33:00 -04:00
|
|
|
$ docker run -itd --network=multi-host-network busybox
|
2015-09-28 21:57:03 -04:00
|
|
|
```
|
|
|
|
|
2022-03-30 09:05:29 -04:00
|
|
|
### <a name=ip></a> Specify the IP address a container will use on a given network (--ip)
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
You can specify the IP address you want to be assigned to the container's interface.
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-01-11 05:34:17 -05:00
|
|
|
$ docker network connect --ip 10.10.36.122 multi-host-network container2
|
|
|
|
```
|
|
|
|
|
2022-03-30 09:05:29 -04:00
|
|
|
### <a name=link></a> Use the legacy `--link` option (--link)
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2016-02-11 18:21:52 -05:00
|
|
|
You can use `--link` option to link another container with a preferred alias
|
2016-01-11 05:34:17 -05:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-01-11 05:34:17 -05:00
|
|
|
$ docker network connect --link container1:c1 multi-host-network container2
|
2016-01-07 19:18:34 -05:00
|
|
|
```
|
|
|
|
|
2022-03-30 09:05:29 -04:00
|
|
|
### <a name=alias></a> Create a network alias for a container (--alias)
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2016-01-08 08:45:56 -05:00
|
|
|
`--alias` option can be used to resolve the container by another name in the network
|
|
|
|
being connected to.
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-01-08 08:45:56 -05:00
|
|
|
$ docker network connect --alias db --alias mysql multi-host-network container2
|
|
|
|
```
|
2017-02-07 18:42:48 -05:00
|
|
|
|
|
|
|
### Network implications of stopping, pausing, or restarting containers
|
|
|
|
|
2015-10-18 19:47:32 -04:00
|
|
|
You can pause, restart, and stop containers that are connected to a network.
|
2016-11-18 15:46:10 -05:00
|
|
|
A container connects to its configured networks when it runs.
|
2016-01-26 09:43:07 -05:00
|
|
|
|
|
|
|
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 `--ip-range` 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.
|
2016-01-11 21:03:40 -05:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-01-11 21:03:40 -05:00
|
|
|
$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
|
|
|
|
```
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-01-11 21:03:40 -05:00
|
|
|
$ docker network connect --ip 172.20.128.2 multi-host-network container2
|
|
|
|
```
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2022-03-30 08:33:44 -04:00
|
|
|
To verify the container is connected, use the `docker network inspect` command.
|
|
|
|
Use `docker network disconnect` to remove a container from the network.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2022-03-30 08:33:44 -04:00
|
|
|
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.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Related commands
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2015-10-18 19:47:32 -04: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)
|
2016-10-18 00:36:52 -04:00
|
|
|
* [network prune](network_prune.md)
|
Developer Certificate of Origin
Version 1.1
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Signed-off-by: nkwangleiGIT <nkwanglei@126.com>
2018-05-16 08:16:08 -04:00
|
|
|
* [Understand Docker container networks](https://docs.docker.com/network/)
|
|
|
|
* [Work with networks](https://docs.docker.com/network/bridge/)
|