Skip to content

Commit f4c39e6

Browse files
committed
container: Add Platform field and deprecate OS
Change the persistent container metadata to store the whole platform (as defined by OCI) instead of only the operating system. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 6147606 commit f4c39e6

10 files changed

Lines changed: 37 additions & 25 deletions

File tree

container/container.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/containerd/containerd/cio"
1717
"github.com/containerd/log"
18+
"github.com/containerd/platforms"
1819
containertypes "github.com/docker/docker/api/types/container"
1920
"github.com/docker/docker/api/types/events"
2021
mounttypes "github.com/docker/docker/api/types/mount"
@@ -84,7 +85,11 @@ type Container struct {
8485
LogPath string
8586
Name string
8687
Driver string
87-
OS string
88+
89+
// Deprecated: use [ImagePlatform.OS] instead
90+
OS string
91+
92+
ImagePlatform ocispec.Platform
8893

8994
RestartCount int
9095
HasBeenStartedBefore bool
@@ -162,11 +167,17 @@ func (container *Container) FromDisk() error {
162167
return err
163168
}
164169

165-
// Ensure the operating system is set if blank. Assume it is the OS of the
166-
// host OS if not, to ensure containers created before multiple-OS
167-
// support are migrated
168-
if container.OS == "" {
169-
container.OS = runtime.GOOS
170+
if container.OS != "" {
171+
// OS was deprecated in favor of ImagePlatform
172+
// Make sure we migrate the OS to ImagePlatform.OS.
173+
if container.ImagePlatform.OS == "" {
174+
container.ImagePlatform.OS = container.OS //nolint:staticcheck // ignore SA1019: field is deprecated
175+
}
176+
} else {
177+
// Pre multiple-OS support containers have no OS set.
178+
// Assume it is the host platform.
179+
container.ImagePlatform = platforms.DefaultSpec()
180+
container.OS = container.ImagePlatform.OS //nolint:staticcheck // ignore SA1019: field is deprecated
170181
}
171182

172183
return container.readHostConfig()
@@ -752,7 +763,7 @@ func getConfigTargetPath(r *swarmtypes.ConfigReference) string {
752763
// CreateDaemonEnvironment creates a new environment variable slice for this container.
753764
func (container *Container) CreateDaemonEnvironment(tty bool, linkedEnv []string) []string {
754765
// Setup environment
755-
ctrOS := container.OS
766+
ctrOS := container.ImagePlatform.OS
756767
if ctrOS == "" {
757768
ctrOS = runtime.GOOS
758769
}

daemon/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (daemon *Daemon) CreateImageFromContainer(ctx context.Context, name string,
151151
if c.Config == nil {
152152
c.Config = container.Config
153153
}
154-
newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.OS)
154+
newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.ImagePlatform.OS)
155155
if err != nil {
156156
return "", err
157157
}
@@ -166,7 +166,7 @@ func (daemon *Daemon) CreateImageFromContainer(ctx context.Context, name string,
166166
ContainerConfig: container.Config,
167167
ContainerID: container.ID,
168168
ContainerMountLabel: container.MountLabel,
169-
ContainerOS: container.OS,
169+
ContainerOS: container.ImagePlatform.OS,
170170
ParentImageID: string(container.ImageID),
171171
})
172172
if err != nil {

daemon/container.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
volumemounts "github.com/docker/docker/volume/mounts"
2424
"github.com/docker/go-connections/nat"
2525
"github.com/moby/sys/signal"
26+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2627
"github.com/opencontainers/selinux/go-selinux"
2728
"github.com/pkg/errors"
2829
)
@@ -119,7 +120,7 @@ func (daemon *Daemon) register(ctx context.Context, c *container.Container) erro
119120
return c.CheckpointTo(ctx, daemon.containersReplica)
120121
}
121122

122-
func (daemon *Daemon) newContainer(name string, operatingSystem string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
123+
func (daemon *Daemon) newContainer(name string, platform ocispec.Platform, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
123124
var (
124125
id string
125126
err error
@@ -153,8 +154,9 @@ func (daemon *Daemon) newContainer(name string, operatingSystem string, config *
153154
base.NetworkSettings = &network.Settings{}
154155
base.Name = name
155156
base.Driver = daemon.imageService.StorageDriver()
156-
base.OS = operatingSystem
157-
return base, nil
157+
base.ImagePlatform = platform
158+
base.OS = platform.OS //nolint:staticcheck // ignore SA1019: field is deprecated, but still set for compatibility
159+
return base, err
158160
}
159161

160162
// GetByName returns a container given a name.

daemon/containerd/image_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConf
325325
return "", fmt.Errorf("container not found: %s", c.ContainerID)
326326
}
327327
c.ContainerMountLabel = ctr.MountLabel
328-
c.ContainerOS = ctr.OS
328+
c.ContainerOS = ctr.ImagePlatform.OS
329329
c.ParentImageID = string(ctr.ImageID)
330330
return i.CommitImage(ctx, c)
331331
}

daemon/create.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package daemon // import "github.com/docker/docker/daemon"
33
import (
44
"context"
55
"fmt"
6-
"runtime"
76
"strings"
87
"time"
98

@@ -23,7 +22,7 @@ import (
2322
"github.com/docker/docker/runconfig"
2423
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2524
"github.com/opencontainers/selinux/go-selinux"
26-
archvariant "github.com/tonistiigi/go-archvariant"
25+
"github.com/tonistiigi/go-archvariant"
2726
)
2827

2928
type createOpts struct {
@@ -134,7 +133,7 @@ func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts
134133
imgManifest *ocispec.Descriptor
135134
imgID image.ID
136135
err error
137-
os = runtime.GOOS
136+
platform = platforms.DefaultSpec()
138137
)
139138

140139
if opts.params.Config.Image != "" {
@@ -152,17 +151,17 @@ func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts
152151
return nil, err
153152
}
154153
}
155-
os = img.OperatingSystem()
154+
platform = img.Platform()
156155
imgID = img.ID()
157156
} else if isWindows {
158-
os = "linux" // 'scratch' case.
157+
platform.OS = "linux" // 'scratch' case.
159158
}
160159

161160
// On WCOW, if are not being invoked by the builder to create this container (where
162161
// ignoreImagesArgEscaped will be true) - if the image already has its arguments escaped,
163162
// ensure that this is replicated across to the created container to avoid double-escaping
164163
// of the arguments/command line when the runtime attempts to run the container.
165-
if os == "windows" && !opts.ignoreImagesArgsEscaped && img != nil && img.RunConfig().ArgsEscaped {
164+
if platform.OS == "windows" && !opts.ignoreImagesArgsEscaped && img != nil && img.RunConfig().ArgsEscaped {
166165
opts.params.Config.ArgsEscaped = true
167166
}
168167

@@ -174,7 +173,7 @@ func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts
174173
return nil, errdefs.InvalidParameter(err)
175174
}
176175

177-
if ctr, err = daemon.newContainer(opts.params.Name, os, opts.params.Config, opts.params.HostConfig, imgID, opts.managed); err != nil {
176+
if ctr, err = daemon.newContainer(opts.params.Name, platform, opts.params.Config, opts.params.HostConfig, imgID, opts.managed); err != nil {
178177
return nil, err
179178
}
180179
defer func() {

daemon/exec_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func (daemon *Daemon) execSetPlatformOpt(ctx context.Context, daemonCfg *config.Config, ec *container.ExecConfig, p *specs.Process) error {
12-
if ec.Container.OS == "windows" {
12+
if ec.Container.ImagePlatform.OS == "windows" {
1313
p.User.Username = ec.User
1414
}
1515
return nil

daemon/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (daemon *Daemon) ContainerExport(ctx context.Context, name string, out io.W
2020
return err
2121
}
2222

23-
if isWindows && ctr.OS == "windows" {
23+
if isWindows && ctr.ImagePlatform.OS == "windows" {
2424
return fmt.Errorf("the daemon on this operating system does not support exporting Windows containers")
2525
}
2626

daemon/health.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func getShell(cntr *container.Container) []string {
457457
if runtime.GOOS != "windows" {
458458
return []string{"/bin/sh", "-c"}
459459
}
460-
if cntr.OS != runtime.GOOS {
460+
if cntr.ImagePlatform.OS != runtime.GOOS {
461461
return []string{"/bin/sh", "-c"}
462462
}
463463
return []string{"cmd", "/S", "/C"}

daemon/images/image_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConf
128128
return "", errors.Errorf("container not found: %s", c.ContainerID)
129129
}
130130
c.ContainerMountLabel = ctr.MountLabel
131-
c.ContainerOS = ctr.OS
131+
c.ContainerOS = ctr.ImagePlatform.OS
132132
c.ParentImageID = string(ctr.ImageID)
133133
return i.CommitImage(ctx, c)
134134
}

daemon/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (daemon *Daemon) getInspectData(daemonCfg *config.Config, container *contai
145145
Name: container.Name,
146146
RestartCount: container.RestartCount,
147147
Driver: container.Driver,
148-
Platform: container.OS,
148+
Platform: container.ImagePlatform.OS,
149149
MountLabel: container.MountLabel,
150150
ProcessLabel: container.ProcessLabel,
151151
ExecIDs: container.GetExecIDs(),

0 commit comments

Comments
 (0)