Merge pull request #35419 from johnstep/handle-scratch-image-os
Set OS on scratch image and prevent panic if empty
diff --git a/builder/dockerfile/imagecontext.go b/builder/dockerfile/imagecontext.go
index 79206da..a22b60b 100644
--- a/builder/dockerfile/imagecontext.go
+++ b/builder/dockerfile/imagecontext.go
@@ -1,6 +1,8 @@
package dockerfile
import (
+ "runtime"
+
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/remotecontext"
@@ -73,7 +75,13 @@
func (m *imageSources) Add(im *imageMount) {
switch im.image {
case nil:
- im.image = &dockerimage.Image{}
+ // set the OS for scratch images
+ os := runtime.GOOS
+ // Windows does not support scratch except for LCOW
+ if runtime.GOOS == "windows" {
+ os = "linux"
+ }
+ im.image = &dockerimage.Image{V1Image: dockerimage.V1Image{OS: os}}
default:
m.byImageID[im.image.ImageID()] = im
}
diff --git a/daemon/create.go b/daemon/create.go
index b201447..e4d17cc 100644
--- a/daemon/create.go
+++ b/daemon/create.go
@@ -95,7 +95,14 @@
if err != nil {
return nil, err
}
- os = img.OS
+ if img.OS != "" {
+ os = img.OS
+ } else {
+ // default to the host OS except on Windows with LCOW
+ if runtime.GOOS == "windows" && system.LCOWSupported() {
+ os = "linux"
+ }
+ }
imgID = img.ID()
if runtime.GOOS == "windows" && img.OS == "linux" && !system.LCOWSupported() {
diff --git a/integration-cli/docker_api_build_test.go b/integration-cli/docker_api_build_test.go
index 8c494f1..3f43c95 100644
--- a/integration-cli/docker_api_build_test.go
+++ b/integration-cli/docker_api_build_test.go
@@ -619,6 +619,28 @@
return
}
+func (s *DockerSuite) TestBuildScratchCopy(c *check.C) {
+ testRequires(c, DaemonIsLinux)
+ dockerfile := `FROM scratch
+ADD Dockerfile /
+ENV foo bar`
+ ctx := fakecontext.New(c, "",
+ fakecontext.WithDockerfile(dockerfile),
+ )
+ defer ctx.Close()
+
+ res, body, err := request.Post(
+ "/build",
+ request.RawContent(ctx.AsTarReader(c)),
+ request.ContentType("application/x-tar"))
+ c.Assert(err, checker.IsNil)
+ c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
+
+ out, err := request.ReadBody(body)
+ require.NoError(c, err)
+ assert.Contains(c, string(out), "Successfully built")
+}
+
type buildLine struct {
Stream string
Aux struct {