(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts, globalOptions types.GlobalCommandOptions)
| 78 | ) |
| 79 | |
| 80 | func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts, globalOptions types.GlobalCommandOptions) (digest.Digest, error) { |
| 81 | // Get labels |
| 82 | containerLabels, err := container.Labels(ctx) |
| 83 | if err != nil { |
| 84 | return emptyDigest, err |
| 85 | } |
| 86 | |
| 87 | // Get datastore |
| 88 | dataStore, err := clientutil.DataStore(globalOptions.DataRoot, globalOptions.Address) |
| 89 | if err != nil { |
| 90 | return emptyDigest, err |
| 91 | } |
| 92 | |
| 93 | // Ensure we do have a stateDir label |
| 94 | stateDir := containerLabels[labels.StateDir] |
| 95 | if stateDir == "" { |
| 96 | stateDir, err = containerutil.ContainerStateDirPath(globalOptions.Namespace, dataStore, container.ID()) |
| 97 | if err != nil { |
| 98 | return emptyDigest, err |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | lf, err := containerutil.Lock(stateDir) |
| 103 | if err != nil { |
| 104 | return emptyDigest, err |
| 105 | } |
| 106 | defer lf.Release() |
| 107 | |
| 108 | id := container.ID() |
| 109 | info, err := container.Info(ctx) |
| 110 | if err != nil { |
| 111 | return emptyDigest, err |
| 112 | } |
| 113 | |
| 114 | // NOTE: Moby uses provided rootfs to run container. It doesn't support |
| 115 | // to commit container created by moby. |
| 116 | baseImgWithoutPlatform, err := client.ImageService().Get(ctx, info.Image) |
| 117 | if err != nil { |
| 118 | return emptyDigest, fmt.Errorf("container %q lacks image (wasn't created by nerdctl?): %w", id, err) |
| 119 | } |
| 120 | platformLabel := info.Labels[labels.Platform] |
| 121 | if platformLabel == "" { |
| 122 | platformLabel = platforms.DefaultString() |
| 123 | log.G(ctx).Warnf("Image lacks label %q, assuming the platform to be %q", labels.Platform, platformLabel) |
| 124 | } |
| 125 | ocispecPlatform, err := platforms.Parse(platformLabel) |
| 126 | if err != nil { |
| 127 | return emptyDigest, err |
| 128 | } |
| 129 | log.G(ctx).Debugf("ocispecPlatform=%q", platforms.Format(ocispecPlatform)) |
| 130 | platformMC := platforms.Only(ocispecPlatform) |
| 131 | baseImg := containerd.NewImageWithPlatform(client, baseImgWithoutPlatform, platformMC) |
| 132 | |
| 133 | baseImgConfig, _, err := imgutil.ReadImageConfig(ctx, baseImg) |
| 134 | if err != nil { |
| 135 | return emptyDigest, err |
| 136 | } |
| 137 |
no test coverage detected
searching dependent graphs…