func (container *Container) allocateNetwork() error { mode := container.hostConfig.NetworkMode if container.Config.NetworkDisabled || mode.IsContainer() || mode.IsHost() { return nil } var ( env *engine.Env err error eng = container.daemon.eng ) job := eng.Job("allocate_interface", container.ID) if env, err = job.Stdout.AddEnv(); err != nil { return err } if err := job.Run(); err != nil { return err } if container.Config.PortSpecs != nil { if err := migratePortMappings(container.Config, container.hostConfig); err != nil { return err } container.Config.PortSpecs = nil if err := container.WriteHostConfig(); err != nil { return err } } var ( portSpecs = make(nat.PortSet) bindings = make(nat.PortMap) ) if container.Config.ExposedPorts != nil { portSpecs = container.Config.ExposedPorts } if container.hostConfig.PortBindings != nil { bindings = container.hostConfig.PortBindings } container.NetworkSettings.PortMapping = nil for port := range portSpecs { if err := container.allocatePort(eng, port, bindings); err != nil { return err } } container.WriteHostConfig() container.NetworkSettings.Ports = bindings container.NetworkSettings.Bridge = env.Get("Bridge") container.NetworkSettings.IPAddress = env.Get("IP") container.NetworkSettings.IPPrefixLen = env.GetInt("IPPrefixLen") container.NetworkSettings.Gateway = env.Get("Gateway") return nil }
func waitForExit(cli *DockerCli, containerId string) (int, error) { stream, _, err := cli.call("POST", "/containers/"+containerId+"/wait", nil, false) if err != nil { return -1, err } var out engine.Env if err := out.Decode(stream); err != nil { return -1, err } return out.GetInt("StatusCode"), nil }
func TestPostContainersWait(t *testing.T) { eng := NewTestEngine(t) defer mkRuntimeFromEngine(eng, t).Nuke() srv := mkServerFromEngine(eng, t) containerID := createTestContainer(eng, &docker.Config{ Image: unitTestImageID, Cmd: []string{"/bin/sleep", "1"}, OpenStdin: true, }, t, ) startContainer(eng, containerID, t) setTimeout(t, "Wait timed out", 3*time.Second, func() { r := httptest.NewRecorder() req, err := http.NewRequest("POST", "/containers/"+containerID+"/wait", bytes.NewReader([]byte{})) if err != nil { t.Fatal(err) } if err := docker.ServeRequest(srv, docker.APIVERSION, r, req); err != nil { t.Fatal(err) } assertHttpNotError(r, t) var apiWait engine.Env if err := apiWait.Decode(r.Body); err != nil { t.Fatal(err) } if apiWait.GetInt("StatusCode") != 0 { t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.GetInt("StatusCode")) } }) if containerRunning(eng, containerID, t) { t.Fatalf("The container should be stopped after wait") } }
func (container *Container) allocateNetwork() error { if container.Config.NetworkDisabled { return nil } var ( env *engine.Env err error eng = container.runtime.eng ) if container.State.IsGhost() { if container.runtime.config.DisableNetwork { env = &engine.Env{} } else { currentIP := container.NetworkSettings.IPAddress job := eng.Job("allocate_interface", container.ID) if currentIP != "" { job.Setenv("RequestIP", currentIP) } env, err = job.Stdout.AddEnv() if err != nil { return err } if err := job.Run(); err != nil { return err } } } else { job := eng.Job("allocate_interface", container.ID) env, err = job.Stdout.AddEnv() if err != nil { return err } if err := job.Run(); err != nil { return err } } if container.Config.PortSpecs != nil { utils.Debugf("Migrating port mappings for container: %s", strings.Join(container.Config.PortSpecs, ", ")) if err := migratePortMappings(container.Config, container.hostConfig); err != nil { return err } container.Config.PortSpecs = nil if err := container.writeHostConfig(); err != nil { return err } } var ( portSpecs = make(nat.PortSet) bindings = make(nat.PortMap) ) if !container.State.IsGhost() { if container.Config.ExposedPorts != nil { portSpecs = container.Config.ExposedPorts } if container.hostConfig.PortBindings != nil { bindings = container.hostConfig.PortBindings } } else { if container.NetworkSettings.Ports != nil { for port, binding := range container.NetworkSettings.Ports { portSpecs[port] = struct{}{} bindings[port] = binding } } } container.NetworkSettings.PortMapping = nil for port := range portSpecs { binding := bindings[port] if container.hostConfig.PublishAllPorts && len(binding) == 0 { binding = append(binding, nat.PortBinding{}) } for i := 0; i < len(binding); i++ { b := binding[i] portJob := eng.Job("allocate_port", container.ID) portJob.Setenv("HostIP", b.HostIp) portJob.Setenv("HostPort", b.HostPort) portJob.Setenv("Proto", port.Proto()) portJob.Setenv("ContainerPort", port.Port()) portEnv, err := portJob.Stdout.AddEnv() if err != nil { return err } if err := portJob.Run(); err != nil { eng.Job("release_interface", container.ID).Run() return err } b.HostIp = portEnv.Get("HostIP") b.HostPort = portEnv.Get("HostPort") binding[i] = b } bindings[port] = binding } container.writeHostConfig() container.NetworkSettings.Ports = bindings container.NetworkSettings.Bridge = env.Get("Bridge") container.NetworkSettings.IPAddress = env.Get("IP") container.NetworkSettings.IPPrefixLen = env.GetInt("IPPrefixLen") container.NetworkSettings.Gateway = env.Get("Gateway") return nil }