MCPcopy
hub / github.com/containerd/containerd / StartContainer

Method StartContainer

internal/cri/server/container_start.go:50–277  ·  view source on GitHub ↗

StartContainer starts the container.

(ctx context.Context, r *runtime.StartContainerRequest)

Source from the content-addressed store, hash-verified

48
49// StartContainer starts the container.
50func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContainerRequest) (retRes *runtime.StartContainerResponse, retErr error) {
51 span := tracing.SpanFromContext(ctx)
52 start := time.Now()
53 cntr, err := c.containerStore.Get(r.GetContainerId())
54 if err != nil {
55 return nil, fmt.Errorf("an error occurred when try to find container %q: %w", r.GetContainerId(), err)
56 }
57 span.SetAttributes(tracing.Attribute("container.id", cntr.ID))
58 info, err := cntr.Container.Info(ctx)
59 if err != nil {
60 return nil, fmt.Errorf("get container info: %w", err)
61 }
62
63 id := cntr.ID
64 meta := cntr.Metadata
65 container := cntr.Container
66 config := meta.Config
67
68 // Set starting state to prevent other start/remove operations against this container
69 // while it's being started.
70 if err := setContainerStarting(cntr); err != nil {
71 return nil, fmt.Errorf("failed to set starting state for container %q: %w", id, err)
72 }
73 defer func() {
74 if retErr != nil {
75 // Set container to exited if fail to start.
76 if err := cntr.Status.UpdateSync(func(status containerstore.Status) (containerstore.Status, error) {
77 status.Pid = 0
78 status.FinishedAt = time.Now().UnixNano()
79 status.ExitCode = errorStartExitCode
80 status.Reason = errorStartReason
81 status.Message = retErr.Error()
82 return status, nil
83 }); err != nil {
84 log.G(ctx).WithError(err).Errorf("failed to set start failure state for container %q", id)
85 }
86 }
87 if err := resetContainerStarting(cntr); err != nil {
88 log.G(ctx).WithError(err).Errorf("failed to reset starting state for container %q", id)
89 }
90 }()
91
92 // Get sandbox config from sandbox store.
93 sandbox, err := c.sandboxStore.Get(meta.SandboxID)
94 if err != nil {
95 return nil, fmt.Errorf("sandbox %q not found: %w", meta.SandboxID, err)
96 }
97 sandboxID := meta.SandboxID
98 if sandbox.Status.Get().State != sandboxstore.StateReady {
99 return nil, fmt.Errorf("sandbox container %q is not running", sandboxID)
100 }
101 span.SetAttributes(tracing.Attribute("sandbox.id", sandboxID))
102
103 ioCreation := func(id string) (_ containerdio.IO, err error) {
104 stdoutWC, stderrWC, err := c.createContainerLoggers(meta.LogPath, config.GetTty())
105 if err != nil {
106 return nil, fmt.Errorf("failed to create container loggers: %w", err)
107 }

Callers

nothing calls this directly

Implementers 1

criServiceinternal/cri/server/service.go

Calls 15

getContainerRootDirMethod · 0.95
SpanFromContextFunction · 0.92
AttributeFunction · 0.92
setContainerStartingFunction · 0.85
resetContainerStartingFunction · 0.85
SetAttributesMethod · 0.80
AddOutputMethod · 0.80
PipeMethod · 0.80

Tested by

no test coverage detected