Skip to content

Commit 6078b4d

Browse files
kianelbondeloof
authored andcommitted
Fix: use image created time when last tag time is not present
Signed-off-by: Kian Eliasi <[email protected]>
1 parent 73e593e commit 6078b4d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

cmd/compose/images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service,
103103
for ctr, i := range images {
104104
lastTagTime := i.LastTagTime
105105
if lastTagTime.IsZero() {
106-
lastTagTime = time.Now()
106+
lastTagTime = i.Created
107107
}
108108
imageList = append(imageList, img{
109109
ContainerName: ctr,

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ type ImageSummary struct {
558558
Tag string
559559
Platform platforms.Platform
560560
Size int64
561+
Created time.Time
561562
LastTagTime time.Time
562563
}
563564

pkg/compose/images.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"slices"
2323
"strings"
2424
"sync"
25+
"time"
2526

2627
"github.com/containerd/errdefs"
2728
"github.com/containerd/platforms"
@@ -90,6 +91,11 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
9091
}
9192
}
9293

94+
created, err := time.Parse(time.RFC3339Nano, image.Created)
95+
if err != nil {
96+
return err
97+
}
98+
9399
mux.Lock()
94100
defer mux.Unlock()
95101
summary[getCanonicalContainerName(c)] = api.ImageSummary{
@@ -103,6 +109,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
103109
Variant: image.Variant,
104110
},
105111
Size: image.Size,
112+
Created: created,
106113
LastTagTime: image.Metadata.LastTagTime,
107114
}
108115
return nil

pkg/compose/images_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"strings"
2222
"testing"
23+
"time"
2324

2425
"github.com/docker/docker/api/types"
2526
"github.com/docker/docker/api/types/container"
@@ -44,8 +45,12 @@ func TestImages(t *testing.T) {
4445
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))
4546
listOpts := container.ListOptions{All: true, Filters: args}
4647
api.EXPECT().ServerVersion(gomock.Any()).Return(types.Version{APIVersion: "1.96"}, nil).AnyTimes()
47-
image1 := imageInspect("image1", "foo:1", 12345)
48-
image2 := imageInspect("image2", "bar:2", 67890)
48+
timeStr1 := "2025-06-06T06:06:06.000000000Z"
49+
created1, _ := time.Parse(time.RFC3339Nano, timeStr1)
50+
timeStr2 := "2025-03-03T03:03:03.000000000Z"
51+
created2, _ := time.Parse(time.RFC3339Nano, timeStr2)
52+
image1 := imageInspect("image1", "foo:1", 12345, timeStr1)
53+
image2 := imageInspect("image2", "bar:2", 67890, timeStr2)
4954
api.EXPECT().ImageInspect(anyCancellableContext(), "foo:1").Return(image1, nil).MaxTimes(2)
5055
api.EXPECT().ImageInspect(anyCancellableContext(), "bar:2").Return(image2, nil)
5156
c1 := containerDetail("service1", "123", "running", "foo:1")
@@ -62,32 +67,36 @@ func TestImages(t *testing.T) {
6267
Repository: "foo",
6368
Tag: "1",
6469
Size: 12345,
70+
Created: created1,
6571
},
6672
"456": {
6773
ID: "image2",
6874
Repository: "bar",
6975
Tag: "2",
7076
Size: 67890,
77+
Created: created2,
7178
},
7279
"789": {
7380
ID: "image1",
7481
Repository: "foo",
7582
Tag: "1",
7683
Size: 12345,
84+
Created: created1,
7785
},
7886
}
7987
assert.NilError(t, err)
8088
assert.DeepEqual(t, images, expected)
8189
}
8290

83-
func imageInspect(id string, imageReference string, size int64) image.InspectResponse {
91+
func imageInspect(id string, imageReference string, size int64, created string) image.InspectResponse {
8492
return image.InspectResponse{
8593
ID: id,
8694
RepoTags: []string{
8795
"someRepo:someTag",
8896
imageReference,
8997
},
90-
Size: size,
98+
Size: size,
99+
Created: created,
91100
}
92101
}
93102

0 commit comments

Comments
 (0)