Browse code

Use image ID if tag has been updated

Fixes #12595

Signed-off-by: Travis Thieman <travis.thieman@gmail.com>

Travis Thieman authored on 2015/05/01 10:13:18
Showing 2 changed files
... ...
@@ -136,7 +136,18 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container,
136 136
 			ID:    container.ID,
137 137
 			Names: names[container.ID],
138 138
 		}
139
-		newC.Image = container.Config.Image
139
+
140
+		img, err := daemon.Repositories().LookupImage(container.Config.Image)
141
+		if err != nil {
142
+			// If the image can no longer be found by its original reference,
143
+			// it makes sense to show the ID instead of a stale reference.
144
+			newC.Image = container.ImageID
145
+		} else if container.ImageID == img.ID {
146
+			newC.Image = container.Config.Image
147
+		} else {
148
+			newC.Image = container.ImageID
149
+		}
150
+
140 151
 		if len(container.Args) > 0 {
141 152
 			args := []string{}
142 153
 			for _, arg := range container.Args {
... ...
@@ -573,3 +573,57 @@ func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
573 573
 		c.Fatalf("Expected to print only the container id, got %v\n", out)
574 574
 	}
575 575
 }
576
+
577
+// Test for GitHub issue #12595
578
+func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
579
+
580
+	originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
581
+	updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
582
+
583
+	runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
584
+	out, _, err := runCommandWithOutput(runCmd)
585
+	c.Assert(err, check.IsNil)
586
+
587
+	originalImageID, err := getIDByName(originalImageName)
588
+	c.Assert(err, check.IsNil)
589
+
590
+	runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top")
591
+	out, _, err = runCommandWithOutput(runCmd)
592
+	c.Assert(err, check.IsNil)
593
+	containerID := strings.TrimSpace(out)
594
+
595
+	linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
596
+	c.Assert(err, check.IsNil)
597
+
598
+	lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
599
+	// skip header
600
+	lines = lines[1:]
601
+	c.Assert(len(lines), check.Equals, 1)
602
+
603
+	for _, line := range lines {
604
+		f := strings.Fields(line)
605
+		c.Assert(f[1], check.Equals, originalImageName)
606
+	}
607
+
608
+	runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
609
+	out, _, err = runCommandWithOutput(runCmd)
610
+	c.Assert(err, check.IsNil)
611
+
612
+	runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
613
+	out, _, err = runCommandWithOutput(runCmd)
614
+	c.Assert(err, check.IsNil)
615
+
616
+	linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
617
+	c.Assert(err, check.IsNil)
618
+
619
+	lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
620
+	// skip header
621
+	lines = lines[1:]
622
+	c.Assert(len(lines), check.Equals, 1)
623
+
624
+	for _, line := range lines {
625
+		f := strings.Fields(line)
626
+		c.Assert(f[1], check.Equals, originalImageID)
627
+	}
628
+
629
+}