github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/docs/reference/commandline/network_create.md (about)

     1  # network create
     2  
     3  <!---MARKER_GEN_START-->
     4  Create a network
     5  
     6  ### Options
     7  
     8  | Name                      | Type          | Default   | Description                                             |
     9  |:--------------------------|:--------------|:----------|:--------------------------------------------------------|
    10  | `--attachable`            |               |           | Enable manual container attachment                      |
    11  | `--aux-address`           | `map`         | `map[]`   | Auxiliary IPv4 or IPv6 addresses used by Network driver |
    12  | `--config-from`           | `string`      |           | The network from which to copy the configuration        |
    13  | `--config-only`           |               |           | Create a configuration only network                     |
    14  | `-d`, `--driver`          | `string`      | `bridge`  | Driver to manage the Network                            |
    15  | `--gateway`               | `stringSlice` |           | IPv4 or IPv6 Gateway for the master subnet              |
    16  | [`--ingress`](#ingress)   |               |           | Create swarm routing-mesh network                       |
    17  | [`--internal`](#internal) |               |           | Restrict external access to the network                 |
    18  | `--ip-range`              | `stringSlice` |           | Allocate container ip from a sub-range                  |
    19  | `--ipam-driver`           | `string`      | `default` | IP Address Management Driver                            |
    20  | `--ipam-opt`              | `map`         | `map[]`   | Set IPAM driver specific options                        |
    21  | `--ipv6`                  |               |           | Enable IPv6 networking                                  |
    22  | `--label`                 | `list`        |           | Set metadata on a network                               |
    23  | `-o`, `--opt`             | `map`         | `map[]`   | Set driver specific options                             |
    24  | `--scope`                 | `string`      |           | Control the network's scope                             |
    25  | `--subnet`                | `stringSlice` |           | Subnet in CIDR format that represents a network segment |
    26  
    27  
    28  <!---MARKER_GEN_END-->
    29  
    30  ## Description
    31  
    32  Creates a new network. The `DRIVER` accepts `bridge` or `overlay` which are the
    33  built-in network drivers. If you have installed a third party or your own custom
    34  network driver you can specify that `DRIVER` here also. If you don't specify the
    35  `--driver` option, the command automatically creates a `bridge` network for you.
    36  When you install Docker Engine it creates a `bridge` network automatically. This
    37  network corresponds to the `docker0` bridge that Docker Engine has traditionally relied
    38  on. When you launch a new container with  `docker run` it automatically connects to
    39  this bridge network. You cannot remove this default bridge network, but you can
    40  create new ones using the `network create` command.
    41  
    42  ```console
    43  $ docker network create -d bridge my-bridge-network
    44  ```
    45  
    46  Bridge networks are isolated networks on a single Docker Engine installation. If you
    47  want to create a network that spans multiple Docker hosts each running Docker
    48  Engine, you must enable Swarm mode, and create an `overlay` network. To read more
    49  about overlay networks with Swarm mode, see ["*use overlay networks*"](https://docs.docker.com/network/overlay/).
    50  
    51  Once you have enabled swarm mode, you can create a swarm-scoped overlay network:
    52  
    53  ```console
    54  $ docker network create --scope=swarm --attachable -d overlay my-multihost-network
    55  ```
    56  
    57  By default, swarm-scoped networks do not allow manually started containers to
    58  be attached. This restriction is added to prevent someone that has access to
    59  a non-manager node in the swarm cluster from running a container that is able
    60  to access the network stack of a swarm service.
    61  
    62  The `--attachable` option used in the example above disables this restriction,
    63  and allows for both swarm services and manually started containers to attach to
    64  the overlay network.
    65  
    66  Network names must be unique. The Docker daemon attempts to identify naming
    67  conflicts but this is not guaranteed. It is the user's responsibility to avoid
    68  name conflicts.
    69  
    70  ### Overlay network limitations
    71  
    72  You should create overlay networks with `/24` blocks (the default), which limits
    73  you to 256 IP addresses, when you create networks using the default VIP-based
    74  endpoint-mode. This recommendation addresses
    75  [limitations with swarm mode](https://github.com/moby/moby/issues/30820). If you
    76  need more than 256 IP addresses, do not increase the IP block size. You can
    77  either use `dnsrr` endpoint mode with an external load balancer, or use multiple
    78  smaller overlay networks. See
    79  [Configure service discovery](https://docs.docker.com/engine/swarm/networking/#configure-service-discovery)
    80  for more information about different endpoint modes.
    81  
    82  ## Examples
    83  
    84  ### Connect containers
    85  
    86  When you start a container, use the `--network` flag to connect it to a network.
    87  This example adds the `busybox` container to the `mynet` network:
    88  
    89  ```console
    90  $ docker run -itd --network=mynet busybox
    91  ```
    92  
    93  If you want to add a container to a network after the container is already
    94  running, use the `docker network connect` subcommand.
    95  
    96  You can connect multiple containers to the same network. Once connected, the
    97  containers can communicate using only another container's IP address or name.
    98  For `overlay` networks or custom plugins that support multi-host connectivity,
    99  containers connected to the same multi-host network but launched from different
   100  daemons can also communicate in this way.
   101  
   102  You can disconnect a container from a network using the `docker network
   103  disconnect` command.
   104  
   105  ### Specify advanced options
   106  
   107  When you create a network, Docker Engine creates a non-overlapping subnetwork
   108  for the network by default. This subnetwork is not a subdivision of an existing
   109  network. It is purely for ip-addressing purposes. You can override this default
   110  and specify subnetwork values directly using the `--subnet` option. On a
   111  `bridge` network you can only create a single subnet:
   112  
   113  ```console
   114  $ docker network create --driver=bridge --subnet=192.168.0.0/16 br0
   115  ```
   116  
   117  Additionally, you also specify the `--gateway` `--ip-range` and `--aux-address`
   118  options.
   119  
   120  ```console
   121  $ docker network create \
   122    --driver=bridge \
   123    --subnet=172.28.0.0/16 \
   124    --ip-range=172.28.5.0/24 \
   125    --gateway=172.28.5.254 \
   126    br0
   127  ```
   128  
   129  If you omit the `--gateway` flag, Docker Engine selects one for you from inside
   130  a preferred pool. For `overlay` networks and for network driver plugins that
   131  support it you can create multiple subnetworks. This example uses two `/25`
   132  subnet mask to adhere to the current guidance of not having more than 256 IPs in
   133  a single overlay network. Each of the subnetworks has 126 usable addresses.
   134  
   135  ```console
   136  $ docker network create -d overlay \
   137    --subnet=192.168.10.0/25 \
   138    --subnet=192.168.20.0/25 \
   139    --gateway=192.168.10.100 \
   140    --gateway=192.168.20.100 \
   141    --aux-address="my-router=192.168.10.5" --aux-address="my-switch=192.168.10.6" \
   142    --aux-address="my-printer=192.168.20.5" --aux-address="my-nas=192.168.20.6" \
   143    my-multihost-network
   144  ```
   145  
   146  Be sure that your subnetworks do not overlap. If they do, the network create
   147  fails and Docker Engine returns an error.
   148  
   149  ### Bridge driver options
   150  
   151  When creating a custom network, the default network driver (i.e. `bridge`) has
   152  additional options that can be passed. The following are those options and the
   153  equivalent Docker daemon flags used for docker0 bridge:
   154  
   155  | Option                                           | Equivalent  | Description                                           |
   156  |--------------------------------------------------|-------------|-------------------------------------------------------|
   157  | `com.docker.network.bridge.name`                 | -           | Bridge name to be used when creating the Linux bridge |
   158  | `com.docker.network.bridge.enable_ip_masquerade` | `--ip-masq` | Enable IP masquerading                                |
   159  | `com.docker.network.bridge.enable_icc`           | `--icc`     | Enable or Disable Inter Container Connectivity        |
   160  | `com.docker.network.bridge.host_binding_ipv4`    | `--ip`      | Default IP when binding container ports               |
   161  | `com.docker.network.driver.mtu`                  | `--mtu`     | Set the containers network MTU                        |
   162  | `com.docker.network.container_iface_prefix`      | -           | Set a custom prefix for container interfaces          |
   163  
   164  The following arguments can be passed to `docker network create` for any
   165  network driver, again with their approximate equivalents to Docker daemon
   166  flags used for the docker0 bridge:
   167  
   168  | Argument     | Equivalent     | Description                                |
   169  |--------------|----------------|--------------------------------------------|
   170  | `--gateway`  | -              | IPv4 or IPv6 Gateway for the master subnet |
   171  | `--ip-range` | `--fixed-cidr` | Allocate IPs from a range                  |
   172  | `--internal` | -              | Restrict external access to the network    |
   173  | `--ipv6`     | `--ipv6`       | Enable IPv6 networking                     |
   174  | `--subnet`   | `--bip`        | Subnet for network                         |
   175  
   176  For example, let's use `-o` or `--opt` options to specify an IP address binding
   177  when publishing ports:
   178  
   179  ```console
   180  $ docker network create \
   181      -o "com.docker.network.bridge.host_binding_ipv4"="172.19.0.1" \
   182      simple-network
   183  ```
   184  
   185  ### <a name="internal"></a> Network internal mode (--internal)
   186  
   187  Containers on an internal network may communicate between each other, but not
   188  with any other network, as no default route is configured and firewall rules
   189  are set up to drop all traffic to or from other networks. Communication with
   190  the gateway IP  address (and thus appropriately configured host services) is
   191  possible, and the host may communicate with any container IP directly.
   192  
   193  By default, when you connect a container to an `overlay` network, Docker also
   194  connects a bridge network to it to provide external connectivity. If you want
   195  to create an externally isolated `overlay` network, you can specify the
   196  `--internal` option.
   197  
   198  ### <a name="ingress"></a> Network ingress mode (--ingress)
   199  
   200  You can create the network which will be used to provide the routing-mesh in the
   201  swarm cluster. You do so by specifying `--ingress` when creating the network. Only
   202  one ingress network can be created at the time. The network can be removed only
   203  if no services depend on it. Any option available when creating an overlay network
   204  is also available when creating the ingress network, besides the `--attachable` option.
   205  
   206  ```console
   207  $ docker network create -d overlay \
   208    --subnet=10.11.0.0/16 \
   209    --ingress \
   210    --opt com.docker.network.driver.mtu=9216 \
   211    --opt encrypted=true \
   212    my-ingress-network
   213  ```
   214  
   215  ### Run services on predefined networks
   216  
   217  You can create services on the predefined Docker networks `bridge` and `host`.
   218  
   219  ```console
   220  $ docker service create --name my-service \
   221    --network host \
   222    --replicas 2 \
   223    busybox top
   224  ```
   225  
   226  ### Swarm networks with local scope drivers
   227  
   228  You can create a swarm network with local scope network drivers. You do so
   229  by promoting the network scope to `swarm` during the creation of the network.
   230  You will then be able to use this network when creating services.
   231  
   232  ```console
   233  $ docker network create -d bridge \
   234    --scope swarm \
   235    --attachable \
   236    swarm-network
   237  ```
   238  
   239  For network drivers which provide connectivity across hosts (ex. macvlan), if
   240  node specific configurations are needed in order to plumb the network on each
   241  host, you will supply that configuration via a configuration only network.
   242  When you create the swarm scoped network, you will then specify the name of the
   243  network which contains the configuration.
   244  
   245  
   246  ```console
   247  node1$ docker network create --config-only --subnet 192.168.100.0/24 --gateway 192.168.100.115 mv-config
   248  node2$ docker network create --config-only --subnet 192.168.200.0/24 --gateway 192.168.200.202 mv-config
   249  node1$ docker network create -d macvlan --scope swarm --config-from mv-config --attachable swarm-network
   250  ```
   251  
   252  ## Related commands
   253  
   254  * [network inspect](network_inspect.md)
   255  * [network connect](network_connect.md)
   256  * [network disconnect](network_disconnect.md)
   257  * [network ls](network_ls.md)
   258  * [network rm](network_rm.md)
   259  * [network prune](network_prune.md)
   260  * [Understand Docker container networks](https://docs.docker.com/engine/userguide/networking/)