Add comment column in docker history command output

Signed-off-by: David Young <yangboh@cn.ibm.com>
This commit is contained in:
David Young 2015-01-04 14:47:01 +08:00 committed by Tibor Vass
parent e690ad9292
commit bf57339527
6 changed files with 92 additions and 3 deletions

View File

@ -36,7 +36,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
if !*quiet {
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE")
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
}
for _, entry := range history {
@ -53,7 +53,8 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
} else {
fmt.Fprintf(w, "%s\t", utils.Trunc(entry.CreatedBy, 45))
}
fmt.Fprintf(w, "%s", units.HumanSize(float64(entry.Size)))
fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
fmt.Fprintf(w, "%s\n", entry.Comment)
}
fmt.Fprintf(w, "\n")
}

View File

@ -49,6 +49,7 @@ type ImageHistory struct {
CreatedBy string
Tags []string
Size int64
Comment string
}
// DELETE "/images/{name:.*}"

View File

@ -26,11 +26,29 @@ Show the history of when and how an image was created.
Only show numeric IDs. The default is *false*.
# EXAMPLES
<<<<<<< HEAD
$ docker history fedora
IMAGE CREATED CREATED BY SIZE
=======
## Show the history of images created through docker build command
$ sudo docker history fedora
IMAGE CREATED CREATED BY SIZE COMMENT
>>>>>>> Add comment column in docker history command output
105182bb5e8b 5 days ago /bin/sh -c #(nop) ADD file:71356d2ad59aa3119d 372.7 MB
73bd853d2ea5 13 days ago /bin/sh -c #(nop) MAINTAINER Lokesh Mandvekar 0 B
511136ea3c5a 10 months ago 0 B
511136ea3c5a 10 months ago 0 B Imported from -
## Show the history of images created through docker commit command
`docker commit` command accepts a **-m** parameter to provide comment messages to the image. You can see these messages in image history.
$ sudo docker history docker:scm
IMAGE CREATED CREATED BY SIZE COMMENT
2ac9d1098bf1 3 months ago /bin/bash 241.4 MB Added Apache to Fedora base image
88b42ffd1f7c 5 months ago /bin/sh -c #(nop) ADD file:1fd8d7f9f6557cafc7 373.7 MB
c69cab00d6ef 5 months ago /bin/sh -c #(nop) MAINTAINER Lokesh Mandvekar 0 B
511136ea3c5a 19 months ago 0 B Imported from -
# HISTORY
April 2014, Originally compiled by William Henry (whenry at redhat dot com)

View File

@ -1151,6 +1151,7 @@ This will create a new Bash session in the container `ubuntu_bash`.
To see how the `docker:latest` image was built:
<<<<<<< HEAD
$ docker history docker
IMAGE CREATED CREATED BY SIZE
3e23a5875458790b7a806f95f7ec0d0b2a5c1659bfc899c89f939f6d5b8f7094 8 days ago /bin/sh -c #(nop) ENV LC_ALL=C.UTF-8 0 B
@ -1159,6 +1160,26 @@ To see how the `docker:latest` image was built:
4b137612be55ca69776c7f30c2d2dd0aa2e7d72059820abf3e25b629f887a084 6 weeks ago /bin/sh -c #(nop) ADD jessie.tar.xz in / 121 MB
750d58736b4b6cc0f9a9abe8f258cef269e3e9dceced1146503522be9f985ada 6 weeks ago /bin/sh -c #(nop) MAINTAINER Tianon Gravi <admwiggin@gmail.com> - mkimage-debootstrap.sh -t jessie.tar.xz jessie http://http.debian.net/debian 0 B
511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 9 months ago 0 B
=======
$ sudo docker history docker
IMAGE CREATED CREATED BY SIZE COMMENT
3e23a5875458 8 days ago /bin/sh -c #(nop) ENV LC_ALL=C.UTF-8 0 B
8578938dd170 8 days ago /bin/sh -c dpkg-reconfigure locales && loc 1.245 MB
be51b77efb42 8 days ago /bin/sh -c apt-get update && apt-get install 338.3 MB
4b137612be55 6 weeks ago /bin/sh -c #(nop) ADD jessie.tar.xz in / 121 MB
750d58736b4b 6 weeks ago /bin/sh -c #(nop) MAINTAINER Tianon Gravi <ad 0 B
511136ea3c5a 9 months ago 0 B Imported from -
To see how the `docker:apache` image which was commited from a container:
$ sudo docker history docker:scm
IMAGE CREATED CREATED BY SIZE COMMENT
2ac9d1098bf1 3 months ago /bin/bash 241.4 MB Added Apache to Fedora base image
88b42ffd1f7c 5 months ago /bin/sh -c #(nop) ADD file:1fd8d7f9f6557cafc7 373.7 MB
c69cab00d6ef 5 months ago /bin/sh -c #(nop) MAINTAINER Lokesh Mandvekar 0 B
511136ea3c5a 19 months ago 0 B Imported from -
>>>>>>> Add comment column in docker history command output
## images

View File

@ -41,6 +41,7 @@ func (s *TagStore) CmdHistory(job *engine.Job) error {
CreatedBy: strings.Join(img.ContainerConfig.Cmd, " "),
Tags: lookupMap[img.ID],
Size: img.Size,
Comment: img.Comment,
})
return nil
})

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os/exec"
"regexp"
"strings"
"testing"
)
@ -82,3 +83,49 @@ func TestHistoryNonExistentImage(t *testing.T) {
}
logDone("history - history on non-existent image must pass")
}
func TestHistoryImageWithComment(t *testing.T) {
// make a image through docker commit <container id> [ -m messages ]
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil {
t.Fatalf("failed to run container: %s, %v", out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
}
commitCmd := exec.Command(dockerBinary, "commit", "-m=This is a comment", cleanedContainerID)
out, _, err = runCommandWithOutput(commitCmd)
if err != nil {
t.Fatalf("failed to commit container to image: %s, %v", out, err)
}
cleanedImageID := stripTrailingCharacters(out)
deleteContainer(cleanedContainerID)
defer deleteImages(cleanedImageID)
// test docker history <image id> to check comment messages
historyCmd := exec.Command(dockerBinary, "history", cleanedImageID)
out, exitCode, err := runCommandWithOutput(historyCmd)
if err != nil || exitCode != 0 {
t.Fatalf("failed to get image history: %s, %v", out, err)
}
expectedValue := "This is a comment"
outputLine := strings.Split(out, "\n")[1]
outputTabs := regexp.MustCompile(" +").Split(outputLine, -1)
actualValue := outputTabs[len(outputTabs)-1]
if !strings.Contains(actualValue, expectedValue) {
t.Fatalf("Expected comments \"%s\", but found \"%s\"", expectedValue, actualValue)
}
logDone("history - history on image with comment")
}