From a97817b673cbd3bfaf6e752282c4992ac43ff594 Mon Sep 17 00:00:00 2001 From: John Stephens Date: Mon, 6 Nov 2017 18:21:10 -0800 Subject: [PATCH] Set OS on scratch image and prevent panic if empty Signed-off-by: John Stephens --- builder/dockerfile/imagecontext.go | 10 +++++++++- daemon/create.go | 9 ++++++++- integration-cli/docker_api_build_test.go | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/builder/dockerfile/imagecontext.go b/builder/dockerfile/imagecontext.go index 79206dad4d..a22b60b2e5 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) Unmount() (retErr error) { 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 b2014470c4..e4d17cc2df 100644 --- a/daemon/create.go +++ b/daemon/create.go @@ -95,7 +95,14 @@ func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) ( 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 8c494f1427..3f43c95432 100644 --- a/integration-cli/docker_api_build_test.go +++ b/integration-cli/docker_api_build_test.go @@ -619,6 +619,28 @@ func testBuildWithSession(c *check.C, dir, dockerfile string) (outStr string) { 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 {