Browse code

Fix #8048 : make `docker images repository:tag` work

Make command like "docker images ubuntu:14.04" work and filter out the
image with the given tag.

Closes #8048.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/08/08 22:58:48
Showing 5 changed files
... ...
@@ -22,7 +22,7 @@ import (
22 22
 //
23 23
 // Usage: docker images [OPTIONS] [REPOSITORY]
24 24
 func (cli *DockerCli) CmdImages(args ...string) error {
25
-	cmd := Cli.Subcmd("images", []string{"[REPOSITORY]"}, "List images", true)
25
+	cmd := Cli.Subcmd("images", []string{"[REPOSITORY[:TAG]]"}, "List images", true)
26 26
 	quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
27 27
 	all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
28 28
 	noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
... ...
@@ -11,7 +11,7 @@ weight=1
11 11
 
12 12
 # images
13 13
 
14
-    Usage: docker images [OPTIONS] [REPOSITORY]
14
+    Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
15 15
 
16 16
     List images
17 17
 
... ...
@@ -52,6 +52,36 @@ uses up the `VIRTUAL SIZE` listed only once.
52 52
     postgres                  9.3.5               746b819f315e        4 days ago          213.4 MB
53 53
     postgres                  latest              746b819f315e        4 days ago          213.4 MB
54 54
 
55
+### Listing images by name and tag
56
+
57
+The `docker images` command takes an optional `[REPOSITORY[:TAG]]` argument
58
+that restricts the list to images that match the argument. If you specify
59
+`REPOSITORY`but no `TAG`, the `docker images` command lists all images in the
60
+given repository.
61
+
62
+For example, to list all images in the "java" repository, run this command :
63
+
64
+    $ docker images java
65
+    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
66
+    java                8                   308e519aac60        6 days ago          824.5 MB
67
+    java                7                   493d82594c15        3 months ago        656.3 MB
68
+    java                latest              2711b1d6f3aa        5 months ago        603.9 MB
69
+
70
+The `[REPOSITORY[:TAG]]` value must be an "exact match". This means that, for example,
71
+`docker images jav` does not match the image `java`.
72
+
73
+If both `REPOSITORY` and `TAG` are provided, only images matching that
74
+repository and tag are listed.  To find all local images in the "java"
75
+repository with tag "8" you can use:
76
+
77
+    $ docker images java:8
78
+    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
79
+    java                8                   308e519aac60        6 days ago          824.5 MB
80
+
81
+If nothing matches `REPOSITORY[:TAG]`, the list is empty.
82
+
83
+    $ docker images java:0
84
+    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
55 85
 
56 86
 ## Listing the full length image IDs
57 87
 
... ...
@@ -68,13 +68,29 @@ func (s *TagStore) Images(filterArgs, filter string, all bool) ([]*types.Image,
68 68
 	lookup := make(map[string]*types.Image)
69 69
 	s.Lock()
70 70
 	for repoName, repository := range s.Repositories {
71
+		filterTagName := ""
71 72
 		if filter != "" {
72
-			if match, _ := path.Match(filter, repoName); !match {
73
+			filterName := filter
74
+			// Test if the tag was in there, if yes, get the name
75
+			if strings.Contains(filterName, ":") {
76
+				filterWithTag := strings.Split(filter, ":")
77
+				filterName = filterWithTag[0]
78
+				filterTagName = filterWithTag[1]
79
+			}
80
+			if match, _ := path.Match(filterName, repoName); !match {
73 81
 				continue
74 82
 			}
83
+			if filterTagName != "" {
84
+				if _, ok := repository[filterTagName]; !ok {
85
+					continue
86
+				}
87
+			}
75 88
 		}
76 89
 		for ref, id := range repository {
77 90
 			imgRef := utils.ImageReference(repoName, ref)
91
+			if !strings.Contains(imgRef, filterTagName) {
92
+				continue
93
+			}
78 94
 			image, err := s.graph.Get(id)
79 95
 			if err != nil {
80 96
 				logrus.Warnf("couldn't load %s from %s: %s", id, imgRef, err)
... ...
@@ -18,6 +18,39 @@ func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
18 18
 	}
19 19
 }
20 20
 
21
+func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
22
+	_, err := buildImage("imagewithtag:v1",
23
+		`FROM scratch
24
+		MAINTAINER dockerio1`, true)
25
+	c.Assert(err, check.IsNil)
26
+
27
+	_, err = buildImage("imagewithtag:v2",
28
+		`FROM scratch
29
+		MAINTAINER dockerio1`, true)
30
+	c.Assert(err, check.IsNil)
31
+
32
+	out, _ := dockerCmd(c, "images", "imagewithtag:v1")
33
+
34
+	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || strings.Contains(out, "v2") {
35
+		c.Fatal("images should've listed imagewithtag:v1 and not imagewithtag:v2")
36
+	}
37
+
38
+	out, _ = dockerCmd(c, "images", "imagewithtag")
39
+
40
+	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || !strings.Contains(out, "v2") {
41
+		c.Fatal("images should've listed imagewithtag:v1 and imagewithtag:v2")
42
+	}
43
+}
44
+
45
+func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
46
+	out, _ := dockerCmd(c, "images", "busybox:nonexistent")
47
+
48
+	if strings.Contains(out, "busybox") {
49
+		c.Fatal("images should not have listed busybox")
50
+	}
51
+
52
+}
53
+
21 54
 func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
22 55
 	id1, err := buildImage("order:test_a",
23 56
 		`FROM scratch
... ...
@@ -12,7 +12,7 @@ docker-images - List images
12 12
 [**-f**|**--filter**[=*[]*]]
13 13
 [**--no-trunc**[=*false*]]
14 14
 [**-q**|**--quiet**[=*false*]]
15
-[REPOSITORY]
15
+[REPOSITORY[:TAG]]
16 16
 
17 17
 # DESCRIPTION
18 18
 This command lists the images stored in the local Docker repository.
... ...
@@ -61,6 +61,22 @@ The list will contain the image repository name, a tag for the image, and an
61 61
 image ID, when it was created and its virtual size. Columns: REPOSITORY, TAG,
62 62
 IMAGE ID, CREATED, and VIRTUAL SIZE.
63 63
 
64
+The `docker images` command takes an optional `[REPOSITORY[:TAG]]` argument
65
+that restricts the list to images that match the argument. If you specify
66
+`REPOSITORY`but no `TAG`, the `docker images` command lists all images in the
67
+given repository.
68
+
69
+    docker images java
70
+
71
+The `[REPOSITORY[:TAG]]` value must be an "exact match". This means that, for example,
72
+`docker images jav` does not match the image `java`.
73
+
74
+If both `REPOSITORY` and `TAG` are provided, only images matching that
75
+repository and tag are listed.  To find all local images in the "java"
76
+repository with tag "8" you can use:
77
+
78
+    docker images java:8
79
+
64 80
 To get a verbose list of images which contains all the intermediate images
65 81
 used in builds use **-a**:
66 82