mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #21664 from calavera/label_one_layer_images
Apply build labels to images with only a FROM tag.
This commit is contained in:
commit
47fa54aea3
4 changed files with 66 additions and 9 deletions
|
@ -222,6 +222,17 @@ func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context
|
||||||
}
|
}
|
||||||
return "", err
|
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)
|
shortImgID = stringid.TruncateID(b.image)
|
||||||
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID)
|
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID)
|
||||||
if b.options.Remove {
|
if b.options.Remove {
|
||||||
|
|
|
@ -418,7 +418,20 @@ func (b *Builder) processImageFrom(img builder.Image) error {
|
||||||
b.image = img.ImageID()
|
b.image = img.ImageID()
|
||||||
|
|
||||||
if img.RunConfig() != nil {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6680,11 +6680,9 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
|
||||||
_, err := buildImage(name, `
|
_, err := buildImage(name, `
|
||||||
FROM `+minimalBaseImage()+`
|
FROM `+minimalBaseImage()+`
|
||||||
LABEL default foo
|
LABEL default foo
|
||||||
`, false, []string{"--label", testLabel}...)
|
`, false, "--label", testLabel)
|
||||||
|
|
||||||
if err != nil {
|
c.Assert(err, checker.IsNil)
|
||||||
c.Fatal("error building image with labels", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
res := inspectFieldJSON(c, name, "Config.Labels")
|
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) {
|
func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
|
||||||
name := "testbuildlabelcachecommit"
|
name := "testbuildlabelcachecommit"
|
||||||
testLabel := "foo"
|
testLabel := "foo"
|
||||||
|
@ -6713,11 +6733,9 @@ func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
|
||||||
_, err := buildImage(name, `
|
_, err := buildImage(name, `
|
||||||
FROM `+minimalBaseImage()+`
|
FROM `+minimalBaseImage()+`
|
||||||
LABEL default foo
|
LABEL default foo
|
||||||
`, true, []string{"--label", testLabel}...)
|
`, true, "--label", testLabel)
|
||||||
|
|
||||||
if err != nil {
|
c.Assert(err, checker.IsNil)
|
||||||
c.Fatal("error building image with labels", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
res := inspectFieldJSON(c, name, "Config.Labels")
|
res := inspectFieldJSON(c, name, "Config.Labels")
|
||||||
|
|
||||||
|
|
|
@ -890,6 +890,21 @@ func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
||||||
return *m, nil
|
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) {
|
func getIDByName(name string) (string, error) {
|
||||||
return inspectFieldWithError(name, "Id")
|
return inspectFieldWithError(name, "Id")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue