Signed-off-by: David Calavera <david.calavera@gmail.com>
(cherry picked from commit 1a85c8ebbe1ab508bcd47b883b9732c032509503)
| ... | ... |
@@ -236,6 +236,17 @@ func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context |
| 236 | 236 |
} |
| 237 | 237 |
return "", err |
| 238 | 238 |
} |
| 239 |
+ |
|
| 240 |
+ // Commit the layer when there are only one children in |
|
| 241 |
+ // the dockerfile, this is only the `FROM` tag, and |
|
| 242 |
+ // build labels. Otherwise, the new image won't be |
|
| 243 |
+ // labeled properly. |
|
| 244 |
+ // Commit here, so the ID of the final image is reported |
|
| 245 |
+ // properly. |
|
| 246 |
+ if len(b.dockerfile.Children) == 1 && len(b.options.Labels) > 0 {
|
|
| 247 |
+ b.commit("", b.runConfig.Cmd, "")
|
|
| 248 |
+ } |
|
| 249 |
+ |
|
| 239 | 250 |
shortImgID = stringid.TruncateID(b.image) |
| 240 | 251 |
fmt.Fprintf(b.Stdout, " ---> %s\n", shortImgID) |
| 241 | 252 |
if b.options.Remove {
|
| ... | ... |
@@ -413,7 +413,20 @@ func (b *Builder) processImageFrom(img builder.Image) error {
|
| 413 | 413 |
b.image = img.ImageID() |
| 414 | 414 |
|
| 415 | 415 |
if img.RunConfig() != nil {
|
| 416 |
- b.runConfig = img.RunConfig() |
|
| 416 |
+ imgConfig := *img.RunConfig() |
|
| 417 |
+ // inherit runConfig labels from the current |
|
| 418 |
+ // state if they've been set already. |
|
| 419 |
+ // Ensures that images with only a FROM |
|
| 420 |
+ // get the labels populated properly. |
|
| 421 |
+ if b.runConfig.Labels != nil {
|
|
| 422 |
+ if imgConfig.Labels == nil {
|
|
| 423 |
+ imgConfig.Labels = make(map[string]string) |
|
| 424 |
+ } |
|
| 425 |
+ for k, v := range b.runConfig.Labels {
|
|
| 426 |
+ imgConfig.Labels[k] = v |
|
| 427 |
+ } |
|
| 428 |
+ } |
|
| 429 |
+ b.runConfig = &imgConfig |
|
| 417 | 430 |
} |
| 418 | 431 |
} |
| 419 | 432 |
|
| ... | ... |
@@ -6680,11 +6680,9 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
|
| 6680 | 6680 |
_, err := buildImage(name, ` |
| 6681 | 6681 |
FROM `+minimalBaseImage()+` |
| 6682 | 6682 |
LABEL default foo |
| 6683 |
-`, false, []string{"--label", testLabel}...)
|
|
| 6683 |
+`, false, "--label", testLabel) |
|
| 6684 | 6684 |
|
| 6685 |
- if err != nil {
|
|
| 6686 |
- c.Fatal("error building image with labels", err)
|
|
| 6687 |
- } |
|
| 6685 |
+ c.Assert(err, checker.IsNil) |
|
| 6688 | 6686 |
|
| 6689 | 6687 |
res := inspectFieldJSON(c, name, "Config.Labels") |
| 6690 | 6688 |
|
| ... | ... |
@@ -6699,6 +6697,28 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
|
| 6699 | 6699 |
} |
| 6700 | 6700 |
} |
| 6701 | 6701 |
|
| 6702 |
+func (s *DockerSuite) TestBuildLabelOneNode(c *check.C) {
|
|
| 6703 |
+ name := "testbuildlabel" |
|
| 6704 |
+ |
|
| 6705 |
+ _, err := buildImage(name, "FROM busybox", false, "--label", "foo=bar") |
|
| 6706 |
+ |
|
| 6707 |
+ c.Assert(err, checker.IsNil) |
|
| 6708 |
+ |
|
| 6709 |
+ res, err := inspectImage(name, "json .Config.Labels") |
|
| 6710 |
+ c.Assert(err, checker.IsNil) |
|
| 6711 |
+ var labels map[string]string |
|
| 6712 |
+ |
|
| 6713 |
+ if err := json.Unmarshal([]byte(res), &labels); err != nil {
|
|
| 6714 |
+ c.Fatal(err) |
|
| 6715 |
+ } |
|
| 6716 |
+ |
|
| 6717 |
+ v, ok := labels["foo"] |
|
| 6718 |
+ if !ok {
|
|
| 6719 |
+ c.Fatal("label `foo` not found in image")
|
|
| 6720 |
+ } |
|
| 6721 |
+ c.Assert(v, checker.Equals, "bar") |
|
| 6722 |
+} |
|
| 6723 |
+ |
|
| 6702 | 6724 |
func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
|
| 6703 | 6725 |
name := "testbuildlabelcachecommit" |
| 6704 | 6726 |
testLabel := "foo" |
| ... | ... |
@@ -6713,11 +6733,9 @@ func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
|
| 6713 | 6713 |
_, err := buildImage(name, ` |
| 6714 | 6714 |
FROM `+minimalBaseImage()+` |
| 6715 | 6715 |
LABEL default foo |
| 6716 |
-`, true, []string{"--label", testLabel}...)
|
|
| 6716 |
+`, true, "--label", testLabel) |
|
| 6717 | 6717 |
|
| 6718 |
- if err != nil {
|
|
| 6719 |
- c.Fatal("error building image with labels", err)
|
|
| 6720 |
- } |
|
| 6718 |
+ c.Assert(err, checker.IsNil) |
|
| 6721 | 6719 |
|
| 6722 | 6720 |
res := inspectFieldJSON(c, name, "Config.Labels") |
| 6723 | 6721 |
|
| ... | ... |
@@ -886,6 +886,21 @@ func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
|
| 886 | 886 |
return *m, nil |
| 887 | 887 |
} |
| 888 | 888 |
|
| 889 |
+func inspectImage(name, filter string) (string, error) {
|
|
| 890 |
+ args := []string{"inspect", "--type", "image"}
|
|
| 891 |
+ if filter != "" {
|
|
| 892 |
+ format := fmt.Sprintf("{{%s}}", filter)
|
|
| 893 |
+ args = append(args, "-f", format) |
|
| 894 |
+ } |
|
| 895 |
+ args = append(args, name) |
|
| 896 |
+ inspectCmd := exec.Command(dockerBinary, args...) |
|
| 897 |
+ out, exitCode, err := runCommandWithOutput(inspectCmd) |
|
| 898 |
+ if err != nil || exitCode != 0 {
|
|
| 899 |
+ return "", fmt.Errorf("failed to inspect %s: %s", name, out)
|
|
| 900 |
+ } |
|
| 901 |
+ return strings.TrimSpace(out), nil |
|
| 902 |
+} |
|
| 903 |
+ |
|
| 889 | 904 |
func getIDByName(name string) (string, error) {
|
| 890 | 905 |
return inspectFieldWithError(name, "Id") |
| 891 | 906 |
} |