Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions api/types/container/host_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func (n IpcMode) IsHost() bool {
return n == "host"
}

// IsNetNs indicates whether container uses a container network namespace path.
func (n NetworkMode) IsNetNs() bool {
parts := strings.SplitN(string(n), ":", 2)
return len(parts) > 1 && parts[0] == "netns"
}

// IsContainer indicates whether the container uses a container's ipc stack.
func (n IpcMode) IsContainer() bool {
parts := strings.SplitN(string(n), ":", 2)
Expand Down
11 changes: 8 additions & 3 deletions daemon/container_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai
var n libnetwork.Network

mode := container.HostConfig.NetworkMode
if container.Config.NetworkDisabled || mode.IsContainer() {
if container.Config.NetworkDisabled || mode.IsContainer() || mode.IsNetNs() {
return
}

Expand Down Expand Up @@ -497,7 +497,7 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {

updateSettings := false
if len(container.NetworkSettings.Networks) == 0 {
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() || container.HostConfig.NetworkMode.IsNetNs() {
return nil
}

Expand Down Expand Up @@ -650,6 +650,9 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, n libn

func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
start := time.Now()
if container.HostConfig.NetworkMode.IsNetNs() {
return nil
}
if container.HostConfig.NetworkMode.IsContainer() {
return runconfig.ErrConflictSharedNetwork
}
Expand Down Expand Up @@ -863,6 +866,8 @@ func (daemon *Daemon) initializeNetworking(container *container.Container) error
return err
}

// build hostname file based on netns via --network=netns:/path/to/netns ?

return container.BuildHostnameFile()
}

Expand All @@ -889,7 +894,7 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
if daemon.netController == nil {
return
}
if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsNetNs() {
return
}

Expand Down
2 changes: 2 additions & 0 deletions daemon/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ func setNamespaces(daemon *Daemon, s *specs.Spec, c *container.Container) error
nsUser.Path = fmt.Sprintf("/proc/%d/ns/user", nc.State.GetPID())
setNamespace(s, nsUser)
}
} else if c.HostConfig.NetworkMode.IsNetNs() {
ns.Path = parts[1]
} else if c.HostConfig.NetworkMode.IsHost() {
ns.Path = c.NetworkSettings.SandboxKey
}
Expand Down
8 changes: 8 additions & 0 deletions runconfig/hostconfig_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
}
}

if parts[0] == "netns" {
if len(parts) < 2 || parts[1] == "" {
return fmt.Errorf("--net: invalid net mode: invalid netns format netns:/path/to/netns")
} else {
return nil
}
}

if hc.NetworkMode.IsContainer() && c.Hostname != "" {
return ErrConflictNetworkHostname
}
Expand Down