diff --git a/container/container_unix.go b/container/container_unix.go index 37af5064a8..53c19fc2a9 100644 --- a/container/container_unix.go +++ b/container/container_unix.go @@ -20,7 +20,6 @@ import ( "github.com/docker/docker/pkg/nat" "github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/system" - "github.com/docker/docker/runconfig" "github.com/docker/docker/utils" "github.com/docker/docker/volume" "github.com/docker/libnetwork" @@ -374,64 +373,6 @@ func (container *Container) SetupWorkingDirectory() error { return nil } -// DisconnectFromNetwork disconnects a container from a network -func (container *Container) DisconnectFromNetwork(n libnetwork.Network) error { - if !container.Running { - return derr.ErrorCodeNotRunning.WithArgs(container.ID) - } - - if container.HostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() { - return runconfig.ErrConflictHostNetwork - } - - if err := container.disconnectFromNetwork(n); err != nil { - return err - } - - if err := container.ToDiskLocking(); err != nil { - return fmt.Errorf("Error saving container to disk: %v", err) - } - return nil -} - -func (container *Container) disconnectFromNetwork(n libnetwork.Network) error { - var ( - ep libnetwork.Endpoint - sbox libnetwork.Sandbox - ) - - s := func(current libnetwork.Endpoint) bool { - epInfo := current.Info() - if epInfo == nil { - return false - } - if sb := epInfo.Sandbox(); sb != nil { - if sb.ContainerID() == container.ID { - ep = current - sbox = sb - return true - } - } - return false - } - n.WalkEndpoints(s) - - if ep == nil { - return fmt.Errorf("container %s is not connected to the network", container.ID) - } - - if err := ep.Leave(sbox); err != nil { - return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err) - } - - if err := ep.Delete(); err != nil { - return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err) - } - - delete(container.NetworkSettings.Networks, n.Name()) - return nil -} - // appendNetworkMounts appends any network mounts to the array of mount points passed in func appendNetworkMounts(container *Container, volumeMounts []volume.MountPoint) ([]volume.MountPoint, error) { for _, mnt := range container.NetworkMounts() { diff --git a/container/container_windows.go b/container/container_windows.go index 750ec6f02c..90b1bf4896 100644 --- a/container/container_windows.go +++ b/container/container_windows.go @@ -5,7 +5,6 @@ package container import ( "github.com/docker/docker/daemon/execdriver" "github.com/docker/docker/volume" - "github.com/docker/libnetwork" ) // DefaultPathEnv is deliberately empty on Windows as the default path will be set by @@ -26,11 +25,6 @@ func (container *Container) CreateDaemonEnvironment(linkedEnv []string) []string return container.Config.Env } -// DisconnectFromNetwork disconnects a container from the network. -func (container *Container) DisconnectFromNetwork(n libnetwork.Network) error { - return nil -} - // SetupWorkingDirectory initializes the container working directory. // This is a NOOP In windows. func (container *Container) SetupWorkingDirectory() error { diff --git a/daemon/container_operations_unix.go b/daemon/container_operations_unix.go index 30f1f62138..f660bd7593 100644 --- a/daemon/container_operations_unix.go +++ b/daemon/container_operations_unix.go @@ -657,6 +657,62 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName return nil } +// DisconnectFromNetwork disconnects container from network n. +func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network) error { + if !container.Running { + return derr.ErrorCodeNotRunning.WithArgs(container.ID) + } + + if container.HostConfig.NetworkMode.IsHost() && runconfig.NetworkMode(n.Type()).IsHost() { + return runconfig.ErrConflictHostNetwork + } + + return disconnectFromNetwork(container, n) +} + +func disconnectFromNetwork(container *container.Container, n libnetwork.Network) error { + + if err := container.ToDiskLocking(); err != nil { + return fmt.Errorf("Error saving container to disk: %v", err) + } + + var ( + ep libnetwork.Endpoint + sbox libnetwork.Sandbox + ) + + s := func(current libnetwork.Endpoint) bool { + epInfo := current.Info() + if epInfo == nil { + return false + } + if sb := epInfo.Sandbox(); sb != nil { + if sb.ContainerID() == container.ID { + ep = current + sbox = sb + return true + } + } + return false + } + n.WalkEndpoints(s) + + if ep == nil { + return fmt.Errorf("container %s is not connected to the network", container.ID) + } + + if err := ep.Leave(sbox); err != nil { + return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err) + } + + if err := ep.Delete(); err != nil { + return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err) + } + + delete(container.NetworkSettings.Networks, n.Name()) + return nil +} + func (daemon *Daemon) initializeNetworking(container *container.Container) error { var err error diff --git a/daemon/container_operations_windows.go b/daemon/container_operations_windows.go index b4d148731c..4198c250b1 100644 --- a/daemon/container_operations_windows.go +++ b/daemon/container_operations_windows.go @@ -9,6 +9,7 @@ import ( "github.com/docker/docker/daemon/execdriver" derr "github.com/docker/docker/errors" "github.com/docker/docker/layer" + "github.com/docker/libnetwork" ) func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) { @@ -24,6 +25,11 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName return nil } +// DisconnectFromNetwork disconnects a container from the network. +func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network) error { + return nil +} + func (daemon *Daemon) populateCommand(c *container.Container, env []string) error { en := &execdriver.Network{ Interface: nil, diff --git a/daemon/network.go b/daemon/network.go index e1ac494b9b..a177ee4f77 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -144,7 +144,7 @@ func (daemon *Daemon) DisconnectContainerFromNetwork(containerName string, netwo if err != nil { return err } - return container.DisconnectFromNetwork(network) + return daemon.DisconnectFromNetwork(container, network) } // GetNetworkDriverList returns the list of plugins drivers