Browse code

Set OS on scratch image and prevent panic if empty

Signed-off-by: John Stephens <johnstep@docker.com>

John Stephens authored on 2017/11/07 11:21:10
Showing 3 changed files
... ...
@@ -1,6 +1,8 @@
1 1
 package dockerfile
2 2
 
3 3
 import (
4
+	"runtime"
5
+
4 6
 	"github.com/docker/docker/api/types/backend"
5 7
 	"github.com/docker/docker/builder"
6 8
 	"github.com/docker/docker/builder/remotecontext"
... ...
@@ -73,7 +75,13 @@ func (m *imageSources) Unmount() (retErr error) {
73 73
 func (m *imageSources) Add(im *imageMount) {
74 74
 	switch im.image {
75 75
 	case nil:
76
-		im.image = &dockerimage.Image{}
76
+		// set the OS for scratch images
77
+		os := runtime.GOOS
78
+		// Windows does not support scratch except for LCOW
79
+		if runtime.GOOS == "windows" {
80
+			os = "linux"
81
+		}
82
+		im.image = &dockerimage.Image{V1Image: dockerimage.V1Image{OS: os}}
77 83
 	default:
78 84
 		m.byImageID[im.image.ImageID()] = im
79 85
 	}
... ...
@@ -95,7 +95,14 @@ func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (
95 95
 		if err != nil {
96 96
 			return nil, err
97 97
 		}
98
-		os = img.OS
98
+		if img.OS != "" {
99
+			os = img.OS
100
+		} else {
101
+			// default to the host OS except on Windows with LCOW
102
+			if runtime.GOOS == "windows" && system.LCOWSupported() {
103
+				os = "linux"
104
+			}
105
+		}
99 106
 		imgID = img.ID()
100 107
 
101 108
 		if runtime.GOOS == "windows" && img.OS == "linux" && !system.LCOWSupported() {
... ...
@@ -619,6 +619,28 @@ func testBuildWithSession(c *check.C, dir, dockerfile string) (outStr string) {
619 619
 	return
620 620
 }
621 621
 
622
+func (s *DockerSuite) TestBuildScratchCopy(c *check.C) {
623
+	testRequires(c, DaemonIsLinux)
624
+	dockerfile := `FROM scratch
625
+ADD Dockerfile /
626
+ENV foo bar`
627
+	ctx := fakecontext.New(c, "",
628
+		fakecontext.WithDockerfile(dockerfile),
629
+	)
630
+	defer ctx.Close()
631
+
632
+	res, body, err := request.Post(
633
+		"/build",
634
+		request.RawContent(ctx.AsTarReader(c)),
635
+		request.ContentType("application/x-tar"))
636
+	c.Assert(err, checker.IsNil)
637
+	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
638
+
639
+	out, err := request.ReadBody(body)
640
+	require.NoError(c, err)
641
+	assert.Contains(c, string(out), "Successfully built")
642
+}
643
+
622 644
 type buildLine struct {
623 645
 	Stream string
624 646
 	Aux    struct {