Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom bridge support to docker run #6704

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Custom bridge support added to container linking logic.
Docker-DCO-1.1-Signed-off-by: Vishnu Kannan <vishnuk@google.com> (github: vishh)
  • Loading branch information
vishh committed Jul 11, 2014
commit 7a3a37705c9aed652d810076ca35963949d8e43e
9 changes: 6 additions & 3 deletions daemon/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func populateCommand(c *Container, env []string) error {
case "host":
en.HostNetworking = true
case "bridge", "": // empty string to support existing containers
// TODO(vishh): Handle custom bridge here.
if !c.Config.NetworkDisabled {
network := c.NetworkSettings
en.Interface = &execdriver.NetworkInterface{
Expand Down Expand Up @@ -414,9 +413,9 @@ func (container *Container) allocateNetwork() error {

job := eng.Job("allocate_interface", container.ID)
if bridgeToUse := mode.GetNonDefaultBridge(); bridgeToUse != "" {
job.Setenv("RequestedBridge", bridgeToUse)
job.Setenv("RequestedBridge", bridgeToUse)
}

if env, err = job.Stdout.AddEnv(); err != nil {
return err
}
Expand Down Expand Up @@ -1000,9 +999,13 @@ func (container *Container) setupLinkedContainers() ([]string, error) {
return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
}

if container.NetworkSettings.Bridge != child.NetworkSettings.Bridge {
return nil, fmt.Errorf("Cannot link to a container (%s) running in a different bridge (%s)", child.Name, child.NetworkSettings.Bridge)
}
link, err := links.NewLink(
container.NetworkSettings.IPAddress,
child.NetworkSettings.IPAddress,
container.NetworkSettings.Bridge,
linkAlias,
child.Config.Env,
child.Config.ExposedPorts,
Expand Down
5 changes: 3 additions & 2 deletions daemon/networkdriver/bridge/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ func LinkContainers(job *engine.Job) engine.Status {
parentIP = job.Getenv("ParentIP")
ignoreErrors = job.GetenvBool("IgnoreErrors")
ports = job.GetenvList("Ports")
bridge = job.Getenv("Bridge")
)
split := func(p string) (string, string) {
parts := strings.Split(p, "/")
Expand All @@ -484,7 +485,7 @@ func LinkContainers(job *engine.Job) engine.Status {
for _, p := range ports {
port, proto := split(p)
if output, err := iptables.Raw(action, "FORWARD",
"-i", bridgeIface, "-o", bridgeIface,
"-i", bridge, "-o", bridge,
"-p", proto,
"-s", parentIP,
"--dport", port,
Expand All @@ -496,7 +497,7 @@ func LinkContainers(job *engine.Job) engine.Status {
}

if output, err := iptables.Raw(action, "FORWARD",
"-i", bridgeIface, "-o", bridgeIface,
"-i", bridge, "-o", bridge,
"-p", proto,
"-s", childIP,
"--sport", port,
Expand Down
5 changes: 4 additions & 1 deletion links/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
type Link struct {
ParentIP string
ChildIP string
Bridge string
Name string
ChildEnvironment []string
Ports []nat.Port
IsEnabled bool
eng *engine.Engine
}

func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}, eng *engine.Engine) (*Link, error) {
func NewLink(parentIP, childIP, bridge, name string, env []string, exposedPorts map[nat.Port]struct{}, eng *engine.Engine) (*Link, error) {

var (
i int
Expand All @@ -34,6 +35,7 @@ func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.
Name: name,
ChildIP: childIP,
ParentIP: parentIP,
Bridge: bridge,
ChildEnvironment: env,
Ports: ports,
eng: eng,
Expand Down Expand Up @@ -121,6 +123,7 @@ func (l *Link) toggle(action string, ignoreErrors bool) error {

job.Setenv("ParentIP", l.ParentIP)
job.Setenv("ChildIP", l.ChildIP)
job.Setenv("Bridge", l.Bridge)
job.SetenvBool("IgnoreErrors", ignoreErrors)

out := make([]string, len(l.Ports))
Expand Down
7 changes: 5 additions & 2 deletions links/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestLinkNew(t *testing.T) {
ports := make(nat.PortSet)
ports[nat.Port("6379/tcp")] = struct{}{}

link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports, nil)
link, err := NewLink("172.0.17.3", "172.0.17.2", "docker0", "/db/docker", nil, ports, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -65,13 +65,16 @@ func TestLinkNew(t *testing.T) {
t.Fail()
}
}
if link.Bridge != "docker0" {
t.Fail()
}
}

func TestLinkEnv(t *testing.T) {
ports := make(nat.PortSet)
ports[nat.Port("6379/tcp")] = struct{}{}

link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports, nil)
link, err := NewLink("172.0.17.3", "172.0.17.2", "docker0", "/db/docker", []string{"PASSWORD=gordon"}, ports, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion runconfig/hostconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (n NetworkMode) IsNonDefaultBridge() bool {

func (n NetworkMode) GetNonDefaultBridge() string {
parts := strings.SplitN(string(n), ":", 2)
if (len(parts) > 1 && parts[0] == "bridge") {
if len(parts) > 1 && parts[0] == "bridge" {
return parts[1]
}
return ""
Expand Down