1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Update --net flags and container mode

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-05-02 14:06:05 -07:00
parent 7118416aee
commit 2c2cc051d8
4 changed files with 54 additions and 53 deletions

View file

@ -338,6 +338,10 @@ func populateCommand(c *Container, env []string) error {
Interface: nil, Interface: nil,
} }
parts := strings.SplitN(c.hostConfig.NetworkMode, ":", 2)
switch parts[0] {
case "none":
case "bridge":
if !c.Config.NetworkDisabled { if !c.Config.NetworkDisabled {
network := c.NetworkSettings network := c.NetworkSettings
en.Interface = &execdriver.NetworkInterface{ en.Interface = &execdriver.NetworkInterface{
@ -347,18 +351,19 @@ func populateCommand(c *Container, env []string) error {
IPPrefixLen: network.IPPrefixLen, IPPrefixLen: network.IPPrefixLen,
} }
} }
case "container":
nc := c.daemon.Get(parts[1])
if nc == nil {
return fmt.Errorf("no such container to join network: %q", parts[1])
}
en.ContainerID = nc.ID
default:
return fmt.Errorf("invalid network mode: %s", c.hostConfig.NetworkMode)
}
// TODO: this can be removed after lxc-conf is fully deprecated // TODO: this can be removed after lxc-conf is fully deprecated
mergeLxcConfIntoOptions(c.hostConfig, context) mergeLxcConfIntoOptions(c.hostConfig, context)
if netContainer := c.hostConfig.UseContainerNetwork; netContainer != "" {
nc := c.daemon.Get(netContainer)
if nc == nil {
return fmt.Errorf("no such container to join network: %q", netContainer)
}
en.ContainerID = nc.ID
}
resources := &execdriver.Resources{ resources := &execdriver.Resources{
Memory: c.Config.Memory, Memory: c.Config.Memory,
MemorySwap: c.Config.MemorySwap, MemorySwap: c.Config.MemorySwap,

View file

@ -17,7 +17,7 @@ type HostConfig struct {
Dns []string Dns []string
DnsSearch []string DnsSearch []string
VolumesFrom []string VolumesFrom []string
UseContainerNetwork string NetworkMode string
} }
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig { func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
@ -25,6 +25,7 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
ContainerIDFile: job.Getenv("ContainerIDFile"), ContainerIDFile: job.Getenv("ContainerIDFile"),
Privileged: job.GetenvBool("Privileged"), Privileged: job.GetenvBool("Privileged"),
PublishAllPorts: job.GetenvBool("PublishAllPorts"), PublishAllPorts: job.GetenvBool("PublishAllPorts"),
NetworkMode: job.Getenv("NetworkMode"),
} }
job.GetenvJson("LxcConf", &hostConfig.LxcConf) job.GetenvJson("LxcConf", &hostConfig.LxcConf)
job.GetenvJson("PortBindings", &hostConfig.PortBindings) job.GetenvJson("PortBindings", &hostConfig.PortBindings)
@ -43,8 +44,5 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil { if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
hostConfig.VolumesFrom = VolumesFrom hostConfig.VolumesFrom = VolumesFrom
} }
if UseContainerNetwork := job.Getenv("UseContainerNetwork"); UseContainerNetwork != "" {
hostConfig.UseContainerNetwork = UseContainerNetwork
}
return hostConfig return hostConfig
} }

View file

@ -50,7 +50,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)") flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id") flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id")
flNetwork = cmd.Bool([]string{"n", "-networking"}, true, "Enable networking for this container") flNetwork = cmd.Bool([]string{"#n", "#-networking"}, true, "Enable networking for this container")
flPrivileged = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container") flPrivileged = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container")
flPublishAll = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces") flPublishAll = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces")
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep stdin open even if not attached") flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep stdin open even if not attached")
@ -62,7 +62,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID") flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID")
flWorkingDir = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container") flWorkingDir = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
flCpuShares = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)") flCpuShares = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
flNetMode = cmd.String([]string{"#net", "-net"}, "bridge", "Set the Network mode for the container ('bridge': creates a new network stack for the container on the docker bridge, 'disable': disable networking for this container, 'container:name_or_id': reuses another container network stack)") flNetMode = cmd.String([]string{"-net"}, "bridge", "Set the Network mode for the container ('bridge': creates a new network stack for the container on the docker bridge, 'none': no networking for this container, 'container:name_or_id': reuses another container network stack)")
// For documentation purpose // For documentation purpose
_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)") _ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
_ = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container") _ = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
@ -198,7 +198,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
// boo, there's no debug output for docker run // boo, there's no debug output for docker run
//utils.Debugf("Environment variables for the container: %#v", envVariables) //utils.Debugf("Environment variables for the container: %#v", envVariables)
netMode, useContainerNetwork, err := parseNetMode(*flNetMode) netMode, err := parseNetMode(*flNetMode)
if err != nil { if err != nil {
return nil, nil, cmd, fmt.Errorf("-net: invalid net mode: %v", err) return nil, nil, cmd, fmt.Errorf("-net: invalid net mode: %v", err)
} }
@ -210,7 +210,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
ExposedPorts: ports, ExposedPorts: ports,
User: *flUser, User: *flUser,
Tty: *flTty, Tty: *flTty,
NetworkDisabled: !*flNetwork || netMode == "disable", NetworkDisabled: !*flNetwork,
OpenStdin: *flStdin, OpenStdin: *flStdin,
Memory: flMemory, Memory: flMemory,
CpuShares: *flCpuShares, CpuShares: *flCpuShares,
@ -236,7 +236,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
Dns: flDns.GetAll(), Dns: flDns.GetAll(),
DnsSearch: flDnsSearch.GetAll(), DnsSearch: flDnsSearch.GetAll(),
VolumesFrom: flVolumesFrom.GetAll(), VolumesFrom: flVolumesFrom.GetAll(),
UseContainerNetwork: useContainerNetwork, NetworkMode: netMode,
} }
if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit { if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
@ -282,19 +282,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) {
return out, nil return out, nil
} }
func parseNetMode(netMode string) (string, string, error) { func parseNetMode(netMode string) (string, error) {
parts := strings.Split(netMode, ":") parts := strings.Split(netMode, ":")
switch mode := parts[0]; mode { switch mode := parts[0]; mode {
case "bridge", "disable": case "bridge", "none":
return mode, "", nil return mode, nil
case "container": case "container":
var container string
if len(parts) < 2 || parts[1] == "" { if len(parts) < 2 || parts[1] == "" {
return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode) return "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
} }
container = parts[1] return netMode, nil
return mode, container, nil
default: default:
return "", "", fmt.Errorf("invalid netmode: %q", netMode) return "", fmt.Errorf("invalid netmode: %q", netMode)
} }
} }

View file

@ -40,7 +40,7 @@ func TestParseNetMode(t *testing.T) {
} }
for _, to := range testFlags { for _, to := range testFlags {
mode, container, err := parseNetMode(to.flag) mode, err := parseNetMode(to.flag)
if mode != to.mode { if mode != to.mode {
t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode) t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
} }