Browse code

Fix docker ps truncate long image name by default

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/10/02 23:48:35
Showing 6 changed files
... ...
@@ -62,7 +62,9 @@ func (c *containerContext) Image() string {
62 62
 		return "<no image>"
63 63
 	}
64 64
 	if c.trunc {
65
-		return stringutils.Truncate(c.c.Image, 12)
65
+		if stringid.TruncateID(c.c.ImageID) == stringid.TruncateID(c.c.Image) {
66
+			return stringutils.Truncate(c.c.Image, 12)
67
+		}
66 68
 	}
67 69
 	return c.c.Image
68 70
 }
... ...
@@ -26,8 +26,26 @@ func TestContainerPsContext(t *testing.T) {
26 26
 		{types.Container{ID: containerID}, false, containerID, idHeader, ctx.ID},
27 27
 		{types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names},
28 28
 		{types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image},
29
-		{types.Container{Image: "verylongimagename"}, true, "verylongimag", imageHeader, ctx.Image},
29
+		{types.Container{Image: "verylongimagename"}, true, "verylongimagename", imageHeader, ctx.Image},
30 30
 		{types.Container{Image: "verylongimagename"}, false, "verylongimagename", imageHeader, ctx.Image},
31
+		{types.Container{
32
+			Image:   "a5a665ff33eced1e0803148700880edab4",
33
+			ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
34
+		},
35
+			true,
36
+			"a5a665ff33ec",
37
+			imageHeader,
38
+			ctx.Image,
39
+		},
40
+		{types.Container{
41
+			Image:   "a5a665ff33eced1e0803148700880edab4",
42
+			ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
43
+		},
44
+			false,
45
+			"a5a665ff33eced1e0803148700880edab4",
46
+			imageHeader,
47
+			ctx.Image,
48
+		},
31 49
 		{types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
32 50
 		{types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
33 51
 		{types.Container{Created: unix}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
... ...
@@ -127,6 +127,7 @@ type Container struct {
127 127
 	ID         string `json:"Id"`
128 128
 	Names      []string
129 129
 	Image      string
130
+	ImageID    string
130 131
 	Command    string
131 132
 	Created    int64
132 133
 	Ports      []Port
... ...
@@ -288,8 +288,9 @@ func includeContainerInList(container *Container, ctx *listContext) iterationAct
288 288
 // transformContainer generates the container type expected by the docker ps command.
289 289
 func (daemon *Daemon) transformContainer(container *Container, ctx *listContext) (*types.Container, error) {
290 290
 	newC := &types.Container{
291
-		ID:    container.ID,
292
-		Names: ctx.names[container.ID],
291
+		ID:      container.ID,
292
+		Names:   ctx.names[container.ID],
293
+		ImageID: container.ImageID,
293 294
 	}
294 295
 
295 296
 	img, err := daemon.Repositories().LookupImage(container.Config.Image)
... ...
@@ -89,6 +89,7 @@ list of DNS options to be used in the container.
89 89
 * `POST /build` now optionally takes a serialized map of build-time variables.
90 90
 * `GET /events` now includes a `timenano` field, in addition to the existing `time` field.
91 91
 * `GET /info` now lists engine version information.
92
+* `GET /containers/json` will return `ImageID` of the image used by container.
92 93
 
93 94
 ### v1.20 API changes
94 95
 
... ...
@@ -47,6 +47,7 @@ List containers
47 47
                  "Id": "8dfafdbc3a40",
48 48
                  "Names":["/boring_feynman"],
49 49
                  "Image": "ubuntu:latest",
50
+		 "ImageID": "d74508fb6632491cea586a1fd7d748dfc5274cd6fdfedee309ecdcbc2bf5cb82",
50 51
                  "Command": "echo 1",
51 52
                  "Created": 1367854155,
52 53
                  "Status": "Exit 0",
... ...
@@ -63,6 +64,7 @@ List containers
63 63
                  "Id": "9cd87474be90",
64 64
                  "Names":["/coolName"],
65 65
                  "Image": "ubuntu:latest",
66
+		 "ImageID": "d74508fb6632491cea586a1fd7d748dfc5274cd6fdfedee309ecdcbc2bf5cb82",
66 67
                  "Command": "echo 222222",
67 68
                  "Created": 1367854155,
68 69
                  "Status": "Exit 0",
... ...
@@ -75,6 +77,7 @@ List containers
75 75
                  "Id": "3176a2479c92",
76 76
                  "Names":["/sleepy_dog"],
77 77
                  "Image": "ubuntu:latest",
78
+		 "ImageID": "d74508fb6632491cea586a1fd7d748dfc5274cd6fdfedee309ecdcbc2bf5cb82",
78 79
                  "Command": "echo 3333333333333333",
79 80
                  "Created": 1367854154,
80 81
                  "Status": "Exit 0",
... ...
@@ -87,6 +90,7 @@ List containers
87 87
                  "Id": "4cb07b47f9fb",
88 88
                  "Names":["/running_cat"],
89 89
                  "Image": "ubuntu:latest",
90
+		 "ImageID": "d74508fb6632491cea586a1fd7d748dfc5274cd6fdfedee309ecdcbc2bf5cb82",
90 91
                  "Command": "echo 444444444444444444444444444444444",
91 92
                  "Created": 1367854152,
92 93
                  "Status": "Exit 0",