From 2167f40a766d1909443695d96bcecc3d039fb482 Mon Sep 17 00:00:00 2001 From: Travis Thieman Date: Thu, 30 Apr 2015 21:13:18 -0400 Subject: [PATCH] Use image ID if tag has been updated Fixes #12595 Signed-off-by: Travis Thieman --- daemon/list.go | 13 ++++++- integration-cli/docker_cli_ps_test.go | 54 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/daemon/list.go b/daemon/list.go index 1cf7da4c9f..12c8b75d94 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -136,7 +136,18 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, ID: container.ID, Names: names[container.ID], } - newC.Image = container.Config.Image + + img, err := daemon.Repositories().LookupImage(container.Config.Image) + if err != nil { + // If the image can no longer be found by its original reference, + // it makes sense to show the ID instead of a stale reference. + newC.Image = container.ImageID + } else if container.ImageID == img.ID { + newC.Image = container.Config.Image + } else { + newC.Image = container.ImageID + } + if len(container.Args) > 0 { args := []string{} for _, arg := range container.Args { diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index f272a31ea9..6d376719e6 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -573,3 +573,57 @@ func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) { c.Fatalf("Expected to print only the container id, got %v\n", out) } } + +// Test for GitHub issue #12595 +func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) { + + originalImageName := "busybox:TestPsImageIDAfterUpdate-original" + updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated" + + runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName) + out, _, err := runCommandWithOutput(runCmd) + c.Assert(err, check.IsNil) + + originalImageID, err := getIDByName(originalImageName) + c.Assert(err, check.IsNil) + + runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top") + out, _, err = runCommandWithOutput(runCmd) + c.Assert(err, check.IsNil) + containerID := strings.TrimSpace(out) + + linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() + c.Assert(err, check.IsNil) + + lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n") + // skip header + lines = lines[1:] + c.Assert(len(lines), check.Equals, 1) + + for _, line := range lines { + f := strings.Fields(line) + c.Assert(f[1], check.Equals, originalImageName) + } + + runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName) + out, _, err = runCommandWithOutput(runCmd) + c.Assert(err, check.IsNil) + + runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName) + out, _, err = runCommandWithOutput(runCmd) + c.Assert(err, check.IsNil) + + linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() + c.Assert(err, check.IsNil) + + lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n") + // skip header + lines = lines[1:] + c.Assert(len(lines), check.Equals, 1) + + for _, line := range lines { + f := strings.Fields(line) + c.Assert(f[1], check.Equals, originalImageID) + } + +}