MCPcopy Create free account
hub / github.com/containerd/nerdctl / ContainerFromNative

Function ContainerFromNative

pkg/inspecttypes/dockercompat/dockercompat.go:344–690  ·  view source on GitHub ↗

ContainerFromNative instantiates a Docker-compatible Container from containerd-native Container.

(n *native.Container)

Source from the content-addressed store, hash-verified

342
343// ContainerFromNative instantiates a Docker-compatible Container from containerd-native Container.
344func ContainerFromNative(n *native.Container) (*Container, error) {
345 var hostname string
346 c := &Container{
347 ID: n.ID,
348 Created: n.CreatedAt.Format(time.RFC3339Nano),
349 Image: n.Image,
350 Name: n.Labels[labels.Name],
351 Driver: n.Snapshotter,
352 // XXX is this always right? what if the container OS is NOT the same as the host OS?
353 Platform: runtime.GOOS, // for Docker compatibility, this Platform string does NOT contain arch like "/amd64"
354 }
355 c.HostConfig = new(HostConfig)
356 if n.Labels[restart.StatusLabel] == string(containerd.Running) {
357 c.RestartCount, _ = strconv.Atoi(n.Labels[restart.CountLabel])
358 }
359 containerAnnotations := make(map[string]string)
360 if sp, ok := n.Spec.(*specs.Spec); ok {
361 containerAnnotations = sp.Annotations
362 if p := sp.Process; p != nil {
363 if len(p.Args) > 0 {
364 c.Path = p.Args[0]
365 if len(p.Args) > 1 {
366 c.Args = p.Args[1:]
367 }
368 }
369 c.AppArmorProfile = p.ApparmorProfile
370 }
371 c.Mounts = mountsFromNative(sp.Mounts)
372 for _, mount := range c.Mounts {
373 if mount.Destination == "/etc/resolv.conf" {
374 c.ResolvConfPath = mount.Source
375 } else if mount.Destination == "/etc/hostname" {
376 c.HostnamePath = mount.Source
377 } else if mount.Destination == "/etc/hosts" {
378 c.HostsPath = mount.Source
379 }
380 }
381 hostname = sp.Hostname
382 }
383 if nerdctlStateDir := n.Labels[labels.StateDir]; nerdctlStateDir != "" {
384 resolvConfPath := filepath.Join(nerdctlStateDir, "resolv.conf")
385 if _, err := os.Stat(resolvConfPath); err == nil {
386 c.ResolvConfPath = resolvConfPath
387 }
388 hostnamePath := filepath.Join(nerdctlStateDir, "hostname")
389 if _, err := os.Stat(hostnamePath); err == nil {
390 c.HostnamePath = hostnamePath
391 }
392 c.LogPath = filepath.Join(nerdctlStateDir, n.ID+"-json.log")
393 if _, err := os.Stat(c.LogPath); err != nil {
394 c.LogPath = ""
395 }
396 hostsPath := filepath.Join(nerdctlStateDir, "hosts")
397 if _, err := os.Stat(hostsPath); err == nil {
398 c.HostsPath = hostsPath
399 }
400 }
401

Callers 2

HandlerMethod · 0.92
TestContainerFromNativeFunction · 0.85

Calls 15

GetMountFunction · 0.92
DecodeIPCLabelFunction · 0.92
NewFunction · 0.92
HealthCheckFromJSONFunction · 0.92
mountsFromNativeFunction · 0.85
parseMountsFunction · 0.85
parseExtraHostsFunction · 0.85
groupAddFromNativeFunction · 0.85
statusFromNativeFunction · 0.85

Tested by 1

TestContainerFromNativeFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…