MCPcopy Create free account
hub / github.com/efficientgo/e2e / Start

Method Start

env_docker.go:404–463  ·  view source on GitHub ↗

Start starts runnable.

()

Source from the content-addressed store, hash-verified

402
403// Start starts runnable.
404func (d *dockerRunnable) Start() (err error) {
405 if d.IsRunning() {
406 return errors.Newf("%v is running. Stop or kill it first to restart.", d.Name())
407 }
408
409 d.logger.Log("Starting", d.Name())
410
411 // In case of any error, if the container was already created, we
412 // have to cleanup removing it. We ignore the error of the "docker rm"
413 // because we don't know if the container was created or not.
414 defer func() {
415 if err != nil {
416 _, _ = d.env.exec("docker", "rm", "--force", d.Name()).CombinedOutput()
417 }
418 }()
419
420 // Make sure the image is available locally; if not wait for it to download.
421 if err := d.prePullImage(context.TODO()); err != nil {
422 return err
423 }
424
425 cmd := d.env.exec("docker", append([]string{"run"}, d.env.buildDockerRunArgs(d.name, d.ports, d.opts)...)...)
426 l := &LinePrefixLogger{prefix: d.Name() + ": ", logger: d.logger}
427 cmd.Stdout = l
428 cmd.Stderr = l
429 if err := cmd.Start(); err != nil {
430 return err
431 }
432 d.usedNetworkName = d.env.networkName
433
434 // Wait until the container has been started.
435 if err := d.waitForRunning(); err != nil {
436 return err
437 }
438
439 if err := d.env.registerStarted(d); err != nil {
440 return err
441 }
442
443 // Get the dynamic local ports mapped to the container.
444 for portName, containerPort := range d.ports {
445 var out []byte
446 out, err = d.env.exec("docker", "port", d.containerName(), strconv.Itoa(containerPort)).CombinedOutput()
447 if err != nil {
448 // Catch init errors.
449 if werr := d.waitForRunning(); werr != nil {
450 return errors.Wrapf(werr, "failed to get mapping for port as container %s exited: %v", d.containerName(), err)
451 }
452 return errors.Wrapf(err, "unable to get mapping for port %d; service: %s; output: %q", containerPort, d.Name(), out)
453 }
454
455 d.hostPorts[portName], err = getDockerPortMapping(out)
456 if err != nil {
457 return errors.Wrapf(err, "unable to get mapping for port %d; service: %s", containerPort, d.Name())
458 }
459 }
460
461 d.logger.Log("Ports for container", d.containerName(), ">> Local ports:", d.ports, "Ports available from host:", d.hostPorts)

Callers

nothing calls this directly

Calls 11

IsRunningMethod · 0.95
NameMethod · 0.95
prePullImageMethod · 0.95
waitForRunningMethod · 0.95
containerNameMethod · 0.95
getDockerPortMappingFunction · 0.85
buildDockerRunArgsMethod · 0.80
registerStartedMethod · 0.80
LogMethod · 0.65
StartMethod · 0.65
execMethod · 0.45

Tested by

no test coverage detected