Skip to content

Commit

Permalink
support specifying network name with docker run --net
Browse files Browse the repository at this point in the history
Currently the only way to use a network created with `docker network create` is
the --publish-service option. If the extra flexibility of services is not
required, then that option has the disadvantage of making the user repeat the
container name. Another problem is that the service created persists after the
container is deleted.

This change enables usage like `docker network create blue; docker run --net
blue mycontainer`.

Signed-off-by: Rich Lane <rlane@bigswitch.com>
  • Loading branch information
rlane committed Jul 23, 2015
1 parent 9e438ff commit 9be0775
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions daemon/container_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ func (container *Container) AllocateNetwork() error {
}
} else if service != "" {
return fmt.Errorf("conflicting options: publishing a service and network mode")
} else if mode.IsNamed() {
networkDriver = controller.Config().Daemon.DefaultDriver
}

if runconfig.NetworkMode(networkDriver).IsBridge() && container.daemon.config.DisableBridge {
Expand Down
5 changes: 4 additions & 1 deletion docs/articles/networking.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Finally, several networking options can only be provided when calling
[Configuring DNS](#dns) and
[Communication between containers](#between-containers)

* `--net=bridge|none|container:NAME_or_ID|host` — see
* `--net=bridge|none|container:NAME_or_ID|host|NAME` — see
[How Docker networks a container](#container-networking)

* `--mac-address=MACADDRESS...` — see
Expand Down Expand Up @@ -948,6 +948,9 @@ values.
leaving you free to build any of the custom configurations explored
in the last few sections of this document.

* `--net=NAME` — Tells Docker to use an existing network created by
`docker network create`.

To get an idea of the steps that are necessary if you use `--net=none`
as described in that last bullet point, here are the commands that you
would run to reach roughly the same configuration as if you had let
Expand Down
1 change: 1 addition & 0 deletions docs/reference/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ of the containers.
'none': no networking for this container
'container:<name|id>': reuses another container network stack
'host': use the host network stack inside the container
'<name>': use a network created by "docker network create"
--add-host="" : Add a line to /etc/hosts (host:IP)
--mac-address="" : Sets the container's Ethernet device's MAC address

Expand Down
20 changes: 10 additions & 10 deletions runconfig/hostconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (

func TestNetworkModeTest(t *testing.T) {
networkModes := map[NetworkMode][]bool{
// private, bridge, host, container, none, default
"": {true, false, false, false, false, false},
"something:weird": {true, false, false, false, false, false},
"bridge": {true, true, false, false, false, false},
DefaultDaemonNetworkMode(): {true, true, false, false, false, false},
"host": {false, false, true, false, false, false},
"container:name": {false, false, false, true, false, false},
"none": {true, false, false, false, true, false},
"default": {true, false, false, false, false, true},
// private, bridge, host, container, none, default, named
"": {true, false, false, false, false, false, false},
"mynet": {true, false, false, false, false, false, true},
"bridge": {true, true, false, false, false, false, false},
DefaultDaemonNetworkMode(): {true, true, false, false, false, false, false},
"host": {false, false, true, false, false, false, false},
"container:name": {false, false, false, true, false, false, false},
"none": {true, false, false, false, true, false, false},
"default": {true, false, false, false, false, true, false},
}
networkModeNames := map[NetworkMode]string{
"": "",
"something:weird": "",
"mynet": "mynet",
"bridge": "bridge",
DefaultDaemonNetworkMode(): "bridge",
"host": "host",
Expand Down
8 changes: 7 additions & 1 deletion runconfig/hostconfig_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (n NetworkMode) NetworkName() string {
return "none"
} else if n.IsDefault() {
return "default"
} else {
return string(n)
}
return ""
}

func (n NetworkMode) IsBridge() bool {
Expand All @@ -50,3 +51,8 @@ func (n NetworkMode) IsContainer() bool {
func (n NetworkMode) IsNone() bool {
return n == "none"
}

func (n NetworkMode) IsNamed() bool {
return !n.IsBridge() && !n.IsHost() && !n.IsContainer() &&
!n.IsNone() && !n.IsDefault()
}
4 changes: 2 additions & 2 deletions runconfig/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func TestNetHostname(t *testing.T) {
if _, _, _, err := parseRun([]string{"--net=container", "img", "cmd"}); err == nil || err.Error() != "--net: invalid net mode: invalid container format container:<name|id>" {
t.Fatalf("Expected error with --net=container, got : %v", err)
}
if _, _, _, err := parseRun([]string{"--net=weird", "img", "cmd"}); err == nil || err.Error() != "--net: invalid net mode: invalid --net: weird" {
t.Fatalf("Expected error with --net=weird, got: %s", err)
if _, _, _, err := parseRun([]string{"--net=mynet", "img", "cmd"}); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}

Expand Down
3 changes: 0 additions & 3 deletions runconfig/parse_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
func parseNetMode(netMode string) (NetworkMode, error) {
parts := strings.Split(netMode, ":")
switch mode := parts[0]; mode {
case "default", "bridge", "none", "host":
case "container":
if len(parts) < 2 || parts[1] == "" {
return "", fmt.Errorf("invalid container format container:<name|id>")
}
default:
return "", fmt.Errorf("invalid --net: %s", netMode)
}
return NetworkMode(netMode), nil
}
Expand Down

0 comments on commit 9be0775

Please sign in to comment.