MCPcopy Create free account
hub / github.com/fsouza/go-dockerclient / startContainer

Method startContainer

testing/server.go:667–716  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

665}
666
667func (s *DockerServer) startContainer(w http.ResponseWriter, r *http.Request) {
668 id := mux.Vars(r)["id"]
669 container, err := s.findContainer(id)
670 if err != nil {
671 http.Error(w, err.Error(), http.StatusNotFound)
672 return
673 }
674 s.cMut.Lock()
675 defer s.cMut.Unlock()
676 defer r.Body.Close()
677 if container.State.Running {
678 http.Error(w, "", http.StatusNotModified)
679 return
680 }
681 var hostConfig *docker.HostConfig
682 err = json.NewDecoder(r.Body).Decode(&hostConfig)
683 if err != nil && !errors.Is(err, io.EOF) {
684 http.Error(w, err.Error(), http.StatusInternalServerError)
685 return
686 }
687 if hostConfig == nil {
688 hostConfig = container.HostConfig
689 } else {
690 container.HostConfig = hostConfig
691 }
692 if hostConfig != nil && len(hostConfig.PortBindings) > 0 {
693 ports := map[docker.Port][]docker.PortBinding{}
694 for key, items := range hostConfig.PortBindings {
695 bindings := make([]docker.PortBinding, len(items))
696 for i := range items {
697 binding := docker.PortBinding{
698 HostIP: items[i].HostIP,
699 HostPort: items[i].HostPort,
700 }
701 if binding.HostIP == "" {
702 binding.HostIP = "0.0.0.0"
703 }
704 if binding.HostPort == "" {
705 binding.HostPort = strconv.Itoa(mathrand.Int() % 0xffff)
706 }
707 bindings[i] = binding
708 }
709 ports[key] = bindings
710 }
711 container.NetworkSettings.Ports = ports
712 }
713 container.State.Running = true
714 container.State.StartedAt = time.Now()
715 s.notify(container)
716}
717
718func (s *DockerServer) stopContainer(w http.ResponseWriter, r *http.Request) {
719 id := mux.Vars(r)["id"]

Callers

nothing calls this directly

Calls 5

findContainerMethod · 0.95
notifyMethod · 0.95
DecodeMethod · 0.80
ErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected