Browse code

Add network interal mode

Signed-off-by: Chun Chen <ramichen@tencent.com>
Signed-off-by: David Calavera <david.calavera@gmail.com>

Chun Chen authored on 2015/12/28 11:15:50
Showing 8 changed files
... ...
@@ -47,6 +47,8 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
47 47
 	cmd.Var(flIpamAux, []string{"-aux-address"}, "auxiliary ipv4 or ipv6 addresses used by Network driver")
48 48
 	cmd.Var(flOpts, []string{"o", "-opt"}, "set driver specific options")
49 49
 
50
+	flInternal := cmd.Bool([]string{"-internal"}, false, "restricts external access to the network")
51
+
50 52
 	cmd.Require(flag.Exact, 1)
51 53
 	err := cmd.ParseFlags(args, true)
52 54
 	if err != nil {
... ...
@@ -72,6 +74,7 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
72 72
 		IPAM:           network.IPAM{Driver: *flIpamDriver, Config: ipamCfg},
73 73
 		Options:        flOpts.GetAll(),
74 74
 		CheckDuplicate: true,
75
+		Internal:       *flInternal,
75 76
 	}
76 77
 
77 78
 	resp, err := cli.client.NetworkCreate(nc)
... ...
@@ -13,7 +13,7 @@ type Backend interface {
13 13
 	GetNetworksByID(partialID string) []libnetwork.Network
14 14
 	GetAllNetworks() []libnetwork.Network
15 15
 	CreateNetwork(name, driver string, ipam network.IPAM,
16
-		options map[string]string) (libnetwork.Network, error)
16
+		options map[string]string, internal bool) (libnetwork.Network, error)
17 17
 	ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
18 18
 	DisconnectContainerFromNetwork(containerName string,
19 19
 		network libnetwork.Network) error
... ...
@@ -92,7 +92,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
92 92
 		warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
93 93
 	}
94 94
 
95
-	nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options)
95
+	nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options, create.Internal)
96 96
 	if err != nil {
97 97
 		return err
98 98
 	}
... ...
@@ -101,7 +101,7 @@ func (daemon *Daemon) GetAllNetworks() []libnetwork.Network {
101 101
 }
102 102
 
103 103
 // CreateNetwork creates a network with the given name, driver and other optional parameters
104
-func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string) (libnetwork.Network, error) {
104
+func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string, internal bool) (libnetwork.Network, error) {
105 105
 	c := daemon.netController
106 106
 	if driver == "" {
107 107
 		driver = c.Config().Daemon.DefaultDriver
... ...
@@ -116,6 +116,9 @@ func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, opti
116 116
 
117 117
 	nwOptions = append(nwOptions, libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf, nil))
118 118
 	nwOptions = append(nwOptions, libnetwork.NetworkOptionDriverOpts(options))
119
+	if internal {
120
+		nwOptions = append(nwOptions, libnetwork.NetworkOptionInternalNetwork())
121
+	}
119 122
 	n, err := c.NewNetwork(driver, name, nwOptions...)
120 123
 	if err != nil {
121 124
 		return nil, err
... ...
@@ -114,6 +114,7 @@ This section lists each version from latest to oldest.  Each listing includes a
114 114
 * `POST /containers/create` now allows you to set the static IPv4 and/or IPv6 address for the container.
115 115
 * `POST /networks/(id)/connect` now allows you to set the static IPv4 and/or IPv6 address for the container.
116 116
 * `GET /info` now includes the number of containers running, stopped, and paused.
117
+* `POST /networks/create` now supports restricting external access to the network by setting the `internal` field.
117 118
 
118 119
 ### v1.21 API changes
119 120
 
... ...
@@ -2985,13 +2985,15 @@ Content-Type: application/json
2985 2985
 
2986 2986
 {
2987 2987
   "Name":"isolated_nw",
2988
-  "Driver":"bridge"
2988
+  "Driver":"bridge",
2989 2989
   "IPAM":{
2990 2990
     "Config":[{
2991 2991
       "Subnet":"172.20.0.0/16",
2992 2992
       "IPRange":"172.20.10.0/24",
2993 2993
       "Gateway":"172.20.10.11"
2994 2994
     }]
2995
+  },
2996
+  "Internal":true
2995 2997
 }
2996 2998
 ```
2997 2999
 
... ...
@@ -18,6 +18,7 @@ parent = "smn_cli"
18 18
     -d --driver=DRIVER       Driver to manage the Network bridge or overlay. The default is bridge.
19 19
     --gateway=[]             ipv4 or ipv6 Gateway for the master subnet
20 20
     --help                   Print usage
21
+    --internal               Restricts external access to the network
21 22
     --ip-range=[]            Allocate container ip from a sub-range
22 23
     --ipam-driver=default    IP Address Management Driver
23 24
     -o --opt=map[]           Set custom network plugin options
... ...
@@ -120,6 +121,11 @@ docker network create -d overlay
120 120
 ```
121 121
 Be sure that your subnetworks do not overlap. If they do, the network create fails and Engine returns an error.
122 122
 
123
+### Network internal mode
124
+
125
+By default, when you connect a container to an `overlay` network, Docker also connects a bridge network to it to provide external connectivity.
126
+If you want to create an externally isolated `overlay` network, you can specify the `--internal` option.
127
+
123 128
 ## Related information
124 129
 
125 130
 * [network inspect](network_inspect.md)
... ...
@@ -10,6 +10,7 @@ docker-network-create - create a new network
10 10
 [**-d**|**--driver**=*DRIVER*]
11 11
 [**--gateway**=*[]*]
12 12
 [**--help**]
13
+[**--internal**]
13 14
 [**--ip-range**=*[]*]
14 15
 [**--ipam-driver**=*default*]
15 16
 [**-o**|**--opt**=*map[]*]
... ...
@@ -120,6 +121,11 @@ docker network create -d overlay
120 120
 ```
121 121
 Be sure that your subnetworks do not overlap. If they do, the network create fails and Engine returns an error.
122 122
 
123
+### Network internal mode
124
+
125
+By default, when you connect a container to an `overlay` network, Docker also connects a bridge network to it to provide external connectivity.
126
+If you want to create an externally isolated `overlay` network, you can specify the `--internal` option.
127
+
123 128
 # OPTIONS
124 129
 **--aux-address**=map[]
125 130
   Auxiliary ipv4 or ipv6 addresses used by network driver
... ...
@@ -133,6 +139,9 @@ Be sure that your subnetworks do not overlap. If they do, the network create fai
133 133
 **--help**
134 134
   Print usage
135 135
 
136
+**--internal**
137
+  Restricts external access to the network
138
+
136 139
 **--ip-range**=[]
137 140
   Allocate container ip from a sub-range
138 141