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

Method createContainer

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

Source from the content-addressed store, hash-verified

451}
452
453func (s *DockerServer) createContainer(w http.ResponseWriter, r *http.Request) {
454 var config struct {
455 *docker.Config
456 HostConfig *docker.HostConfig
457 }
458 defer r.Body.Close()
459 err := json.NewDecoder(r.Body).Decode(&config)
460 if err != nil {
461 http.Error(w, err.Error(), http.StatusBadRequest)
462 return
463 }
464 name := r.URL.Query().Get("name")
465 if name != "" && !nameRegexp.MatchString(name) {
466 http.Error(w, "Invalid container name", http.StatusInternalServerError)
467 return
468 }
469 imageID, err := s.findImage(config.Image)
470 if err != nil {
471 http.Error(w, err.Error(), http.StatusNotFound)
472 return
473 }
474 ports := map[docker.Port][]docker.PortBinding{}
475 for port := range config.ExposedPorts {
476 ports[port] = []docker.PortBinding{{
477 HostIP: "0.0.0.0",
478 HostPort: strconv.Itoa(mathrand.Int() % 0xffff),
479 }}
480 }
481
482 // the container may not have cmd when using a Dockerfile
483 var path string
484 var args []string
485 if len(config.Cmd) == 1 {
486 path = config.Cmd[0]
487 } else if len(config.Cmd) > 1 {
488 path = config.Cmd[0]
489 args = config.Cmd[1:]
490 }
491
492 generatedID := s.generateID()
493 config.Hostname = generatedID[:12]
494 container := docker.Container{
495 Name: name,
496 ID: generatedID,
497 Created: time.Now(),
498 Path: path,
499 Args: args,
500 Config: config.Config,
501 HostConfig: config.HostConfig,
502 State: docker.State{
503 Running: false,
504 Pid: mathrand.Int() % 50000,
505 ExitCode: 0,
506 },
507 Image: config.Image,
508 NetworkSettings: &docker.NetworkSettings{
509 IPAddress: fmt.Sprintf("172.16.42.%d", mathrand.Int()%250+2),
510 IPPrefixLen: 24,

Callers

nothing calls this directly

Calls 9

findImageMethod · 0.95
generateIDMethod · 0.95
findContainerWithLockMethod · 0.95
addContainerMethod · 0.95
notifyMethod · 0.95
DecodeMethod · 0.80
GetMethod · 0.80
CloseMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected