2015-09-28 21:57:03 -04:00
|
|
|
# network create
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
<!---MARKER_GEN_START-->
|
2016-06-06 08:20:41 -04:00
|
|
|
Create a network
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
### Options
|
|
|
|
|
|
|
|
| Name | Type | Default | Description |
|
|
|
|
|:--------------------------|:--------------|:----------|:--------------------------------------------------------|
|
2024-07-03 02:29:57 -04:00
|
|
|
| `--attachable` | `bool` | | Enable manual container attachment |
|
2023-01-06 13:04:05 -05:00
|
|
|
| `--aux-address` | `map` | `map[]` | Auxiliary IPv4 or IPv6 addresses used by Network driver |
|
|
|
|
| `--config-from` | `string` | | The network from which to copy the configuration |
|
2024-07-03 02:29:57 -04:00
|
|
|
| `--config-only` | `bool` | | Create a configuration only network |
|
2023-01-06 13:04:05 -05:00
|
|
|
| `-d`, `--driver` | `string` | `bridge` | Driver to manage the Network |
|
|
|
|
| `--gateway` | `stringSlice` | | IPv4 or IPv6 Gateway for the master subnet |
|
2024-07-03 02:29:57 -04:00
|
|
|
| [`--ingress`](#ingress) | `bool` | | Create swarm routing-mesh network |
|
|
|
|
| [`--internal`](#internal) | `bool` | | Restrict external access to the network |
|
2023-01-06 13:04:05 -05:00
|
|
|
| `--ip-range` | `stringSlice` | | Allocate container ip from a sub-range |
|
|
|
|
| `--ipam-driver` | `string` | `default` | IP Address Management Driver |
|
|
|
|
| `--ipam-opt` | `map` | `map[]` | Set IPAM driver specific options |
|
2024-07-03 02:29:57 -04:00
|
|
|
| `--ipv6` | `bool` | | Enable or disable IPv6 networking |
|
2023-01-06 13:04:05 -05:00
|
|
|
| `--label` | `list` | | Set metadata on a network |
|
|
|
|
| `-o`, `--opt` | `map` | `map[]` | Set driver specific options |
|
|
|
|
| `--scope` | `string` | | Control the network's scope |
|
|
|
|
| `--subnet` | `stringSlice` | | Subnet in CIDR format that represents a network segment |
|
|
|
|
|
|
|
|
|
|
|
|
<!---MARKER_GEN_END-->
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Description
|
|
|
|
|
2015-10-18 19:47:32 -04:00
|
|
|
Creates a new network. The `DRIVER` accepts `bridge` or `overlay` which are the
|
|
|
|
built-in network drivers. If you have installed a third party or your own custom
|
|
|
|
network driver you can specify that `DRIVER` here also. If you don't specify the
|
|
|
|
`--driver` option, the command automatically creates a `bridge` network for you.
|
|
|
|
When you install Docker Engine it creates a `bridge` network automatically. This
|
2023-12-13 18:06:16 -05:00
|
|
|
network corresponds to the `docker0` bridge that Docker Engine has traditionally relied
|
2016-07-01 19:11:01 -04:00
|
|
|
on. When you launch a new container with `docker run` it automatically connects to
|
|
|
|
this bridge network. You cannot remove this default bridge network, but you can
|
2015-10-18 19:47:32 -04:00
|
|
|
create new ones using the `network create` command.
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2015-10-18 19:47:32 -04:00
|
|
|
$ docker network create -d bridge my-bridge-network
|
2015-09-28 21:57:03 -04:00
|
|
|
```
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2023-12-13 18:06:16 -05:00
|
|
|
Bridge networks are isolated networks on a single Docker Engine installation. If you
|
|
|
|
want to create a network that spans multiple Docker hosts each running Docker
|
2022-05-17 05:00:51 -04:00
|
|
|
Engine, you must enable Swarm mode, and create an `overlay` network. To read more
|
|
|
|
about overlay networks with Swarm mode, see ["*use overlay networks*"](https://docs.docker.com/network/overlay/).
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2022-05-17 05:00:51 -04:00
|
|
|
Once you have enabled swarm mode, you can create a swarm-scoped overlay network:
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2022-05-17 05:00:51 -04:00
|
|
|
$ docker network create --scope=swarm --attachable -d overlay my-multihost-network
|
2015-10-18 19:47:32 -04:00
|
|
|
```
|
|
|
|
|
2022-05-17 05:00:51 -04:00
|
|
|
By default, swarm-scoped networks do not allow manually started containers to
|
|
|
|
be attached. This restriction is added to prevent someone that has access to
|
|
|
|
a non-manager node in the swarm cluster from running a container that is able
|
|
|
|
to access the network stack of a swarm service.
|
|
|
|
|
|
|
|
The `--attachable` option used in the example above disables this restriction,
|
|
|
|
and allows for both swarm services and manually started containers to attach to
|
2023-12-11 16:33:52 -05:00
|
|
|
the overlay network.
|
2022-05-17 05:00:51 -04:00
|
|
|
|
2015-10-18 19:47:32 -04:00
|
|
|
Network names must be unique. The Docker daemon attempts to identify naming
|
|
|
|
conflicts but this is not guaranteed. It is the user's responsibility to avoid
|
|
|
|
name conflicts.
|
|
|
|
|
2018-01-30 18:41:53 -05:00
|
|
|
### Overlay network limitations
|
|
|
|
|
|
|
|
You should create overlay networks with `/24` blocks (the default), which limits
|
|
|
|
you to 256 IP addresses, when you create networks using the default VIP-based
|
|
|
|
endpoint-mode. This recommendation addresses
|
|
|
|
[limitations with swarm mode](https://github.com/moby/moby/issues/30820). If you
|
|
|
|
need more than 256 IP addresses, do not increase the IP block size. You can
|
|
|
|
either use `dnsrr` endpoint mode with an external load balancer, or use multiple
|
|
|
|
smaller overlay networks. See
|
|
|
|
[Configure service discovery](https://docs.docker.com/engine/swarm/networking/#configure-service-discovery)
|
|
|
|
for more information about different endpoint modes.
|
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
### Connect containers
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2016-06-06 19:33:00 -04:00
|
|
|
When you start a container, use the `--network` flag to connect it to a network.
|
2016-07-01 19:11:01 -04:00
|
|
|
This example adds the `busybox` container to the `mynet` 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=mynet busybox
|
2015-09-28 21:57:03 -04:00
|
|
|
```
|
|
|
|
|
2015-10-18 19:47:32 -04:00
|
|
|
If you want to add a container to a network after the container is already
|
2016-07-01 19:11:01 -04:00
|
|
|
running, use the `docker network connect` subcommand.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
|
|
|
You can connect multiple containers to the same network. Once connected, the
|
|
|
|
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
|
2023-12-13 18:06:16 -05:00
|
|
|
daemons can also communicate in this way.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
|
|
|
You can disconnect a container from a network using the `docker network
|
|
|
|
disconnect` command.
|
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
### Specify advanced options
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2023-12-13 18:06:16 -05:00
|
|
|
When you create a network, Docker Engine creates a non-overlapping subnetwork
|
|
|
|
for the network by default. This subnetwork is not a subdivision of an existing
|
|
|
|
network. It is purely for ip-addressing purposes. You can override this default
|
|
|
|
and specify subnetwork values directly using the `--subnet` option. On a
|
2016-06-06 08:20:41 -04:00
|
|
|
`bridge` network you can only create a single subnet:
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-06-06 08:20:41 -04:00
|
|
|
$ docker network create --driver=bridge --subnet=192.168.0.0/16 br0
|
2015-10-18 19:47:32 -04:00
|
|
|
```
|
2016-06-06 08:20:41 -04:00
|
|
|
|
|
|
|
Additionally, you also specify the `--gateway` `--ip-range` and `--aux-address`
|
|
|
|
options.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-06-06 08:20:41 -04:00
|
|
|
$ docker network create \
|
|
|
|
--driver=bridge \
|
|
|
|
--subnet=172.28.0.0/16 \
|
|
|
|
--ip-range=172.28.5.0/24 \
|
|
|
|
--gateway=172.28.5.254 \
|
|
|
|
br0
|
2015-10-18 19:47:32 -04:00
|
|
|
```
|
|
|
|
|
2023-12-13 18:06:16 -05:00
|
|
|
If you omit the `--gateway` flag, Docker Engine selects one for you from inside
|
|
|
|
a preferred pool. For `overlay` networks and for network driver plugins that
|
2018-01-30 18:41:53 -05:00
|
|
|
support it you can create multiple subnetworks. This example uses two `/25`
|
|
|
|
subnet mask to adhere to the current guidance of not having more than 256 IPs in
|
|
|
|
a single overlay network. Each of the subnetworks has 126 usable addresses.
|
2015-10-18 19:47:32 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-06-06 08:20:41 -04:00
|
|
|
$ docker network create -d overlay \
|
2019-12-23 14:04:09 -05:00
|
|
|
--subnet=192.168.10.0/25 \
|
|
|
|
--subnet=192.168.20.0/25 \
|
|
|
|
--gateway=192.168.10.100 \
|
|
|
|
--gateway=192.168.20.100 \
|
|
|
|
--aux-address="my-router=192.168.10.5" --aux-address="my-switch=192.168.10.6" \
|
|
|
|
--aux-address="my-printer=192.168.20.5" --aux-address="my-nas=192.168.20.6" \
|
2015-12-01 00:33:55 -05:00
|
|
|
my-multihost-network
|
2015-10-18 19:47:32 -04:00
|
|
|
```
|
2016-06-06 08:20:41 -04:00
|
|
|
|
|
|
|
Be sure that your subnetworks do not overlap. If they do, the network create
|
2023-12-13 18:06:16 -05:00
|
|
|
fails and Docker Engine returns an error.
|
2015-09-28 21:57:03 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
### Bridge driver options
|
2016-01-18 00:02:10 -05:00
|
|
|
|
2016-06-06 08:20:41 -04:00
|
|
|
When creating a custom network, the default network driver (i.e. `bridge`) has
|
|
|
|
additional options that can be passed. The following are those options and the
|
2023-12-13 18:06:16 -05:00
|
|
|
equivalent Docker daemon flags used for docker0 bridge:
|
2016-01-18 00:02:10 -05:00
|
|
|
|
|
|
|
| Option | Equivalent | Description |
|
|
|
|
|--------------------------------------------------|-------------|-------------------------------------------------------|
|
2020-04-24 06:39:24 -04:00
|
|
|
| `com.docker.network.bridge.name` | - | Bridge name to be used when creating the Linux bridge |
|
2016-01-18 00:02:10 -05:00
|
|
|
| `com.docker.network.bridge.enable_ip_masquerade` | `--ip-masq` | Enable IP masquerading |
|
|
|
|
| `com.docker.network.bridge.enable_icc` | `--icc` | Enable or Disable Inter Container Connectivity |
|
|
|
|
| `com.docker.network.bridge.host_binding_ipv4` | `--ip` | Default IP when binding container ports |
|
2016-09-02 18:39:22 -04:00
|
|
|
| `com.docker.network.driver.mtu` | `--mtu` | Set the containers network MTU |
|
2021-02-26 09:54:14 -05:00
|
|
|
| `com.docker.network.container_iface_prefix` | - | Set a custom prefix for container interfaces |
|
2016-02-11 20:42:15 -05:00
|
|
|
|
2016-06-06 08:20:41 -04:00
|
|
|
The following arguments can be passed to `docker network create` for any
|
2024-03-08 12:11:28 -05:00
|
|
|
network driver, again with their approximate equivalents to Docker daemon
|
|
|
|
flags used for the docker0 bridge:
|
2016-02-23 11:08:09 -05:00
|
|
|
|
|
|
|
| Argument | Equivalent | Description |
|
|
|
|
|--------------|----------------|--------------------------------------------|
|
2016-07-16 03:32:17 -04:00
|
|
|
| `--gateway` | - | IPv4 or IPv6 Gateway for the master subnet |
|
2016-02-23 11:08:09 -05:00
|
|
|
| `--ip-range` | `--fixed-cidr` | Allocate IPs from a range |
|
2022-03-30 08:33:44 -04:00
|
|
|
| `--internal` | - | Restrict external access to the network |
|
2024-06-06 15:02:49 -04:00
|
|
|
| `--ipv6` | `--ipv6` | Enable or disable IPv6 networking |
|
2016-02-23 11:08:09 -05:00
|
|
|
| `--subnet` | `--bip` | Subnet for network |
|
2016-01-18 00:02:10 -05:00
|
|
|
|
2016-06-06 08:20:41 -04:00
|
|
|
For example, let's use `-o` or `--opt` options to specify an IP address binding
|
|
|
|
when publishing ports:
|
2016-01-18 00:02:10 -05:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2016-06-06 08:20:41 -04:00
|
|
|
$ docker network create \
|
|
|
|
-o "com.docker.network.bridge.host_binding_ipv4"="172.19.0.1" \
|
|
|
|
simple-network
|
2016-01-18 00:02:10 -05:00
|
|
|
```
|
|
|
|
|
2023-01-06 13:28:29 -05:00
|
|
|
### <a name="internal"></a> Network internal mode (--internal)
|
2015-12-27 21:15:50 -05:00
|
|
|
|
2024-03-08 12:12:15 -05:00
|
|
|
Containers on an internal network may communicate between each other, but not
|
|
|
|
with any other network, as no default route is configured and firewall rules
|
|
|
|
are set up to drop all traffic to or from other networks. Communication with
|
|
|
|
the gateway IP address (and thus appropriately configured host services) is
|
|
|
|
possible, and the host may communicate with any container IP directly.
|
|
|
|
|
2016-06-06 08:20:41 -04:00
|
|
|
By default, when you connect a container to an `overlay` network, Docker also
|
|
|
|
connects a bridge network to it to provide external connectivity. If you want
|
|
|
|
to create an externally isolated `overlay` network, you can specify the
|
|
|
|
`--internal` option.
|
2015-12-27 21:15:50 -05:00
|
|
|
|
2023-01-06 13:28:29 -05:00
|
|
|
### <a name="ingress"></a> Network ingress mode (--ingress)
|
2017-03-09 14:52:25 -05:00
|
|
|
|
|
|
|
You can create the network which will be used to provide the routing-mesh in the
|
|
|
|
swarm cluster. You do so by specifying `--ingress` when creating the network. Only
|
|
|
|
one ingress network can be created at the time. The network can be removed only
|
2017-10-31 06:21:09 -04:00
|
|
|
if no services depend on it. Any option available when creating an overlay network
|
2017-03-09 14:52:25 -05:00
|
|
|
is also available when creating the ingress network, besides the `--attachable` option.
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2017-03-09 14:52:25 -05:00
|
|
|
$ docker network create -d overlay \
|
|
|
|
--subnet=10.11.0.0/16 \
|
|
|
|
--ingress \
|
2017-08-11 21:44:36 -04:00
|
|
|
--opt com.docker.network.driver.mtu=9216 \
|
2017-03-09 14:52:25 -05:00
|
|
|
--opt encrypted=true \
|
|
|
|
my-ingress-network
|
|
|
|
```
|
|
|
|
|
2022-05-17 05:00:51 -04:00
|
|
|
### Run services on predefined networks
|
|
|
|
|
2023-12-13 18:06:16 -05:00
|
|
|
You can create services on the predefined Docker networks `bridge` and `host`.
|
2022-05-17 05:00:51 -04:00
|
|
|
|
|
|
|
```console
|
|
|
|
$ docker service create --name my-service \
|
|
|
|
--network host \
|
|
|
|
--replicas 2 \
|
|
|
|
busybox top
|
|
|
|
```
|
|
|
|
|
|
|
|
### Swarm networks with local scope drivers
|
|
|
|
|
|
|
|
You can create a swarm network with local scope network drivers. You do so
|
|
|
|
by promoting the network scope to `swarm` during the creation of the network.
|
|
|
|
You will then be able to use this network when creating services.
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ docker network create -d bridge \
|
|
|
|
--scope swarm \
|
|
|
|
--attachable \
|
|
|
|
swarm-network
|
|
|
|
```
|
|
|
|
|
|
|
|
For network drivers which provide connectivity across hosts (ex. macvlan), if
|
|
|
|
node specific configurations are needed in order to plumb the network on each
|
|
|
|
host, you will supply that configuration via a configuration only network.
|
|
|
|
When you create the swarm scoped network, you will then specify the name of the
|
|
|
|
network which contains the configuration.
|
|
|
|
|
|
|
|
|
|
|
|
```console
|
|
|
|
node1$ docker network create --config-only --subnet 192.168.100.0/24 --gateway 192.168.100.115 mv-config
|
|
|
|
node2$ docker network create --config-only --subnet 192.168.200.0/24 --gateway 192.168.200.202 mv-config
|
|
|
|
node1$ docker network create -d macvlan --scope swarm --config-from mv-config --attachable swarm-network
|
|
|
|
```
|
|
|
|
|
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 connect](network_connect.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)
|
2016-10-20 23:24:28 -04:00
|
|
|
* [Understand Docker container networks](https://docs.docker.com/engine/userguide/networking/)
|