Apply build labels to images with only a FROM tag.

Signed-off-by: David Calavera <david.calavera@gmail.com>
(cherry picked from commit 1a85c8ebbe)
This commit is contained in:
David Calavera 2016-03-30 17:26:02 -04:00 committed by Tibor Vass
parent c5769cf53b
commit 2936442f9d
4 changed files with 66 additions and 9 deletions

View File

@ -236,6 +236,17 @@ func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context
}
return "", err
}
// Commit the layer when there are only one children in
// the dockerfile, this is only the `FROM` tag, and
// build labels. Otherwise, the new image won't be
// labeled properly.
// Commit here, so the ID of the final image is reported
// properly.
if len(b.dockerfile.Children) == 1 && len(b.options.Labels) > 0 {
b.commit("", b.runConfig.Cmd, "")
}
shortImgID = stringid.TruncateID(b.image)
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID)
if b.options.Remove {

View File

@ -413,7 +413,20 @@ func (b *Builder) processImageFrom(img builder.Image) error {
b.image = img.ImageID()
if img.RunConfig() != nil {
b.runConfig = img.RunConfig()
imgConfig := *img.RunConfig()
// inherit runConfig labels from the current
// state if they've been set already.
// Ensures that images with only a FROM
// get the labels populated properly.
if b.runConfig.Labels != nil {
if imgConfig.Labels == nil {
imgConfig.Labels = make(map[string]string)
}
for k, v := range b.runConfig.Labels {
imgConfig.Labels[k] = v
}
}
b.runConfig = &imgConfig
}
}

View File

@ -6680,11 +6680,9 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
_, err := buildImage(name, `
FROM `+minimalBaseImage()+`
LABEL default foo
`, false, []string{"--label", testLabel}...)
`, false, "--label", testLabel)
if err != nil {
c.Fatal("error building image with labels", err)
}
c.Assert(err, checker.IsNil)
res := inspectFieldJSON(c, name, "Config.Labels")
@ -6699,6 +6697,28 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
}
}
func (s *DockerSuite) TestBuildLabelOneNode(c *check.C) {
name := "testbuildlabel"
_, err := buildImage(name, "FROM busybox", false, "--label", "foo=bar")
c.Assert(err, checker.IsNil)
res, err := inspectImage(name, "json .Config.Labels")
c.Assert(err, checker.IsNil)
var labels map[string]string
if err := json.Unmarshal([]byte(res), &labels); err != nil {
c.Fatal(err)
}
v, ok := labels["foo"]
if !ok {
c.Fatal("label `foo` not found in image")
}
c.Assert(v, checker.Equals, "bar")
}
func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
name := "testbuildlabelcachecommit"
testLabel := "foo"
@ -6713,11 +6733,9 @@ func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
_, err := buildImage(name, `
FROM `+minimalBaseImage()+`
LABEL default foo
`, true, []string{"--label", testLabel}...)
`, true, "--label", testLabel)
if err != nil {
c.Fatal("error building image with labels", err)
}
c.Assert(err, checker.IsNil)
res := inspectFieldJSON(c, name, "Config.Labels")

View File

@ -886,6 +886,21 @@ func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
return *m, nil
}
func inspectImage(name, filter string) (string, error) {
args := []string{"inspect", "--type", "image"}
if filter != "" {
format := fmt.Sprintf("{{%s}}", filter)
args = append(args, "-f", format)
}
args = append(args, name)
inspectCmd := exec.Command(dockerBinary, args...)
out, exitCode, err := runCommandWithOutput(inspectCmd)
if err != nil || exitCode != 0 {
return "", fmt.Errorf("failed to inspect %s: %s", name, out)
}
return strings.TrimSpace(out), nil
}
func getIDByName(name string) (string, error) {
return inspectFieldWithError(name, "Id")
}