Browse code

api: omit legacy fields from image inspect if not set

The image inspect response has various fields that are either optional,
or only used if the image was built using the deprecated legacy builder.
This patch marks them as "omitempty" to omit them from the response if
not set:

- The `Parent` field is only used for the legacy builder, and only set for
images that are built locally (i.e., not persisted when pulling an image).
- The `Comment` field is optional, and may not be set, depending on how the
image is produced.
- The `DockerVersion` field is only set when building images with the legacy
builder, and empty in most cases.
- The `Author` field can be set through the `MAINTAINER` instruction in
Dockerfiles, and through the `--author` option on `docker commit`, but
is optional, and won't be set in most situations.

With this patch:

On API v1.52

DOCKER_API_VERSION=v1.52 docker inspect busybox
[
{
"Id": "sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e",
"RepoTags": ["busybox:latest"],
"RepoDigests": ["busybox@sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e"],
"Created": "2024-09-26T21:31:42Z",
"Config": {
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
"Cmd": ["sh"]
},
"Architecture": "arm64",
"Variant": "v8",
"Os": "linux",
"Size": 1913388,
"RootFS": {
"Type": "layers",
"Layers": ["sha256:6aba5e0d32d91e3e923854dcb30588dc4112bfa1dae82b89535ad31d322a7b19"]
},
"Metadata": {
"LastTagTime": "2025-10-03T22:24:18.440035424Z"
},
"Descriptor": {
"mediaType": "application/vnd.oci.image.index.v1+json",
"digest": "sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e",
"size": 9535
}
}
]

On API v1.51 or lower:

DOCKER_API_VERSION=v1.51 docker inspect busybox
[
{
"Architecture": "arm64",
"Author": "",
"Cmd": null,
"Comment": "",
"Config": {
"Cmd": ["sh"],
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]
},
"Created": "2024-09-26T21:31:42Z",
"Descriptor": {
"digest": "sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e",
"mediaType": "application/vnd.oci.image.index.v1+json",
"size": 9535
},
"DockerVersion": "",
"Entrypoint": null,
"Env": null,
"Id": "sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e",
"Labels": null,
"Metadata": {
"LastTagTime": "2025-10-03T22:24:18.440035424Z"
},
"OnBuild": null,
"Os": "linux",
"Parent": "",
"RepoDigests": ["busybox@sha256:d82f458899c9696cb26a7c02d5568f81c8c8223f8661bb2a7988b269c8b9051e"],
"RepoTags": ["busybox:latest"],
"RootFS": {
"Layers": ["sha256:6aba5e0d32d91e3e923854dcb30588dc4112bfa1dae82b89535ad31d322a7b19"],
"Type": "layers"
},
"Size": 1913388,
"User": "",
"Variant": "v8",
"Volumes": null,
"WorkingDir": ""
}
]

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/09/30 23:57:35
Showing 6 changed files
... ...
@@ -34,6 +34,12 @@ keywords: "API, Docker, rcli, REST, documentation"
34 34
 * `GET /images/{name}/json` now omits the following `Config` fields when
35 35
   not set, to closer align with the implementation of the [OCI Image Specification](https://github.com/opencontainers/image-spec/blob/v1.1.1/specs-go/v1/config.go#L23-L62)
36 36
   `Cmd`, `Entrypoint`, `Env`, `Labels`, `OnBuild`, `User`, `Volumes`, and `WorkingDir`.
37
+* `GET /images/{name}/json` now omits the following fields if their value
38
+  is empty: `Parent`, `Comment`, `DockerVersion`, `Author`. The `Parent`
39
+  and `DockerVersion` fields were set by the legacy builder, and are no
40
+  longer set when using BuildKit. The `Author` field is set through the
41
+  `MAINTAINER` Dockerfile instruction, which is deprecated, and the `Comment`
42
+  field is option, and may not be set depending on how the image was created.
37 43
 * `GET /containers/{id}/json`: the `NetworkSettings` no longer returns the deprecated
38 44
   `Bridge`, `HairpinMode`, `LinkLocalIPv6Address`, `LinkLocalIPv6PrefixLen`,
39 45
   `SecondaryIPAddresses`, `SecondaryIPv6Addresses`, `EndpointID`, `Gateway`,
... ...
@@ -1826,13 +1826,13 @@ definitions:
1826 1826
           is only set for images that were built/created locally. This field
1827 1827
           is empty if the image was pulled from an image registry.
1828 1828
         type: "string"
1829
-        x-nullable: false
1829
+        x-nullable: true
1830 1830
         example: ""
1831 1831
       Comment:
1832 1832
         description: |
1833 1833
           Optional message that was set when committing or importing the image.
1834 1834
         type: "string"
1835
-        x-nullable: false
1835
+        x-nullable: true
1836 1836
         example: ""
1837 1837
       Created:
1838 1838
         description: |
... ...
@@ -1851,14 +1851,14 @@ definitions:
1851 1851
 
1852 1852
           Depending on how the image was created, this field may be empty.
1853 1853
         type: "string"
1854
-        x-nullable: false
1854
+        x-nullable: true
1855 1855
         example: "27.0.1"
1856 1856
       Author:
1857 1857
         description: |
1858 1858
           Name of the author that was specified when committing the image, or as
1859 1859
           specified through MAINTAINER (deprecated) in the Dockerfile.
1860 1860
         type: "string"
1861
-        x-nullable: false
1861
+        x-nullable: true
1862 1862
         example: ""
1863 1863
       Config:
1864 1864
         $ref: "#/definitions/ImageConfig"
... ...
@@ -1839,13 +1839,13 @@ definitions:
1839 1839
           is only set for images that were built/created locally. This field
1840 1840
           is empty if the image was pulled from an image registry.
1841 1841
         type: "string"
1842
-        x-nullable: false
1842
+        x-nullable: true
1843 1843
         example: ""
1844 1844
       Comment:
1845 1845
         description: |
1846 1846
           Optional message that was set when committing or importing the image.
1847 1847
         type: "string"
1848
-        x-nullable: false
1848
+        x-nullable: true
1849 1849
         example: ""
1850 1850
       Created:
1851 1851
         description: |
... ...
@@ -1864,14 +1864,14 @@ definitions:
1864 1864
 
1865 1865
           Depending on how the image was created, this field may be empty.
1866 1866
         type: "string"
1867
-        x-nullable: false
1867
+        x-nullable: true
1868 1868
         example: "27.0.1"
1869 1869
       Author:
1870 1870
         description: |
1871 1871
           Name of the author that was specified when committing the image, or as
1872 1872
           specified through MAINTAINER (deprecated) in the Dockerfile.
1873 1873
         type: "string"
1874
-        x-nullable: false
1874
+        x-nullable: true
1875 1875
         example: ""
1876 1876
       Config:
1877 1877
         $ref: "#/definitions/ImageConfig"
... ...
@@ -47,12 +47,12 @@ type InspectResponse struct {
47 47
 	//
48 48
 	// Depending on how the image was created, this field may be empty and
49 49
 	// is only set for images that were built/created locally. This field
50
-	// is empty if the image was pulled from an image registry.
51
-	Parent string
50
+	// is omitted if the image was pulled from an image registry.
51
+	Parent string `json:",omitempty"`
52 52
 
53 53
 	// Comment is an optional message that can be set when committing or
54
-	// importing the image.
55
-	Comment string
54
+	// importing the image. This field is omitted if not set.
55
+	Comment string `json:",omitempty"`
56 56
 
57 57
 	// Created is the date and time at which the image was created, formatted in
58 58
 	// RFC 3339 nano-seconds (time.RFC3339Nano).
... ...
@@ -79,12 +79,13 @@ type InspectResponse struct {
79 79
 
80 80
 	// DockerVersion is the version of Docker that was used to build the image.
81 81
 	//
82
-	// Depending on how the image was created, this field may be empty.
83
-	DockerVersion string
82
+	// Depending on how the image was created, this field may be omitted.
83
+	DockerVersion string `json:",omitempty"`
84 84
 
85 85
 	// Author is the name of the author that was specified when committing the
86 86
 	// image, or as specified through MAINTAINER (deprecated) in the Dockerfile.
87
-	Author string
87
+	// This field is omitted if not set.
88
+	Author string `json:",omitempty"`
88 89
 	Config *dockerspec.DockerOCIImageConfig
89 90
 
90 91
 	// Architecture is the hardware CPU architecture that the image runs on.
... ...
@@ -418,6 +418,14 @@ func (ir *imageRouter) getImagesByName(ctx context.Context, w http.ResponseWrite
418 418
 		imageInspect.Descriptor = nil
419 419
 	}
420 420
 	if versions.LessThan(version, "1.52") {
421
+		// These fields have "omitempty" on API v1.52 and higher,
422
+		// but older API versions returned them unconditionally.
423
+		legacyOptions = append(legacyOptions, compat.WithExtraFields(map[string]any{
424
+			"Parent":        imageInspect.Parent,
425
+			"Comment":       imageInspect.Comment,
426
+			"DockerVersion": imageInspect.DockerVersion,
427
+			"Author":        imageInspect.Author,
428
+		}))
421 429
 		if versions.LessThan(version, "1.50") {
422 430
 			legacyOptions = append(legacyOptions, compat.WithExtraFields(legacyConfigFields["v1.49"]))
423 431
 		} else {
... ...
@@ -47,12 +47,12 @@ type InspectResponse struct {
47 47
 	//
48 48
 	// Depending on how the image was created, this field may be empty and
49 49
 	// is only set for images that were built/created locally. This field
50
-	// is empty if the image was pulled from an image registry.
51
-	Parent string
50
+	// is omitted if the image was pulled from an image registry.
51
+	Parent string `json:",omitempty"`
52 52
 
53 53
 	// Comment is an optional message that can be set when committing or
54
-	// importing the image.
55
-	Comment string
54
+	// importing the image. This field is omitted if not set.
55
+	Comment string `json:",omitempty"`
56 56
 
57 57
 	// Created is the date and time at which the image was created, formatted in
58 58
 	// RFC 3339 nano-seconds (time.RFC3339Nano).
... ...
@@ -79,12 +79,13 @@ type InspectResponse struct {
79 79
 
80 80
 	// DockerVersion is the version of Docker that was used to build the image.
81 81
 	//
82
-	// Depending on how the image was created, this field may be empty.
83
-	DockerVersion string
82
+	// Depending on how the image was created, this field may be omitted.
83
+	DockerVersion string `json:",omitempty"`
84 84
 
85 85
 	// Author is the name of the author that was specified when committing the
86 86
 	// image, or as specified through MAINTAINER (deprecated) in the Dockerfile.
87
-	Author string
87
+	// This field is omitted if not set.
88
+	Author string `json:",omitempty"`
88 89
 	Config *dockerspec.DockerOCIImageConfig
89 90
 
90 91
 	// Architecture is the hardware CPU architecture that the image runs on.