From f9968fd887513e765d068e026695d6c4581d4bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 6 Mar 2026 15:32:13 -0600 Subject: [PATCH] Add support to get the history of an image --- .../github/dockerjava/api/DockerClient.java | 3 + .../dockerjava/api/DockerClientDelegate.java | 6 ++ .../DelegatingDockerCmdExecFactory.java | 5 + .../api/command/DockerCmdExecFactory.java | 2 + .../api/command/ImageHistoryCmd.java | 31 ++++++ .../dockerjava/api/model/ImageHistory.java | 97 +++++++++++++++++++ .../core/AbstractDockerCmdExecFactory.java | 7 ++ .../dockerjava/core/DockerClientImpl.java | 7 ++ .../core/command/ImageHistoryCmdImpl.java | 42 ++++++++ .../core/exec/ImageHistoryCmdExec.java | 35 +++++++ .../api/model/ImageHistoryTest.java | 90 +++++++++++++++++ .../dockerjava/cmd/ImageHistoryCmdIT.java | 29 ++++++ .../samples/1.22/images/history/history.json | 32 ++++++ 13 files changed, 386 insertions(+) create mode 100644 docker-java-api/src/main/java/com/github/dockerjava/api/command/ImageHistoryCmd.java create mode 100644 docker-java-api/src/main/java/com/github/dockerjava/api/model/ImageHistory.java create mode 100644 docker-java-core/src/main/java/com/github/dockerjava/core/command/ImageHistoryCmdImpl.java create mode 100644 docker-java-core/src/main/java/com/github/dockerjava/core/exec/ImageHistoryCmdExec.java create mode 100644 docker-java/src/test/java/com/github/dockerjava/api/model/ImageHistoryTest.java create mode 100644 docker-java/src/test/java/com/github/dockerjava/cmd/ImageHistoryCmdIT.java create mode 100644 docker-java/src/test/resources/samples/1.22/images/history/history.json diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClient.java b/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClient.java index e5f57e1bb..441decad3 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClient.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClient.java @@ -26,6 +26,7 @@ import com.github.dockerjava.api.command.InspectContainerCmd; import com.github.dockerjava.api.command.InspectExecCmd; import com.github.dockerjava.api.command.InspectImageCmd; +import com.github.dockerjava.api.command.ImageHistoryCmd; import com.github.dockerjava.api.command.InspectNetworkCmd; import com.github.dockerjava.api.command.InspectServiceCmd; import com.github.dockerjava.api.command.InspectSwarmCmd; @@ -142,6 +143,8 @@ public interface DockerClient extends Closeable { InspectImageCmd inspectImageCmd(@Nonnull String imageId); + ImageHistoryCmd imageHistoryCmd(@Nonnull String imageId); + /** * @param name * The name, e.g. "alexec/busybox" or just "busybox" if you want to default. Not null. diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClientDelegate.java b/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClientDelegate.java index 5de64641f..fe1f72670 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClientDelegate.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/DockerClientDelegate.java @@ -26,6 +26,7 @@ import com.github.dockerjava.api.command.InspectContainerCmd; import com.github.dockerjava.api.command.InspectExecCmd; import com.github.dockerjava.api.command.InspectImageCmd; +import com.github.dockerjava.api.command.ImageHistoryCmd; import com.github.dockerjava.api.command.InspectNetworkCmd; import com.github.dockerjava.api.command.InspectServiceCmd; import com.github.dockerjava.api.command.InspectSwarmCmd; @@ -179,6 +180,11 @@ public InspectImageCmd inspectImageCmd(@Nonnull String imageId) { return getDockerClient().inspectImageCmd(imageId); } + @Override + public ImageHistoryCmd imageHistoryCmd(@Nonnull String imageId) { + return getDockerClient().imageHistoryCmd(imageId); + } + @Override public SaveImageCmd saveImageCmd(@Nonnull String name) { return getDockerClient().saveImageCmd(name); diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/command/DelegatingDockerCmdExecFactory.java b/docker-java-api/src/main/java/com/github/dockerjava/api/command/DelegatingDockerCmdExecFactory.java index 161ff2c29..8f102a10e 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/command/DelegatingDockerCmdExecFactory.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/command/DelegatingDockerCmdExecFactory.java @@ -100,6 +100,11 @@ public InspectImageCmd.Exec createInspectImageCmdExec() { return getDockerCmdExecFactory().createInspectImageCmdExec(); } + @Override + public ImageHistoryCmd.Exec createImageHistoryCmdExec() { + return getDockerCmdExecFactory().createImageHistoryCmdExec(); + } + @Override public ListContainersCmd.Exec createListContainersCmdExec() { return getDockerCmdExecFactory().createListContainersCmdExec(); diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java b/docker-java-api/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java index cedf6d40d..29c001737 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java @@ -37,6 +37,8 @@ public interface DockerCmdExecFactory extends Closeable { InspectImageCmd.Exec createInspectImageCmdExec(); + ImageHistoryCmd.Exec createImageHistoryCmdExec(); + ListContainersCmd.Exec createListContainersCmdExec(); CreateContainerCmd.Exec createCreateContainerCmdExec(); diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/command/ImageHistoryCmd.java b/docker-java-api/src/main/java/com/github/dockerjava/api/command/ImageHistoryCmd.java new file mode 100644 index 000000000..d93796ad2 --- /dev/null +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/command/ImageHistoryCmd.java @@ -0,0 +1,31 @@ +package com.github.dockerjava.api.command; + +import java.util.List; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.api.model.ImageHistory; + +/** + * Get the history of an image. + */ +public interface ImageHistoryCmd extends SyncDockerCmd> { + + @CheckForNull + String getImageId(); + + ImageHistoryCmd withImageId(@Nonnull String imageId); + + /** + * @throws NotFoundException + * No such image + */ + @Override + List exec() throws NotFoundException; + + interface Exec extends DockerCmdSyncExec> { + } + +} diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/model/ImageHistory.java b/docker-java-api/src/main/java/com/github/dockerjava/api/model/ImageHistory.java new file mode 100644 index 000000000..fb8f5d95c --- /dev/null +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/model/ImageHistory.java @@ -0,0 +1,97 @@ +package com.github.dockerjava.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +import javax.annotation.CheckForNull; +import java.io.Serializable; +import java.util.List; + +/** + * Represents an individual image layer information in response to the ImageHistory operation. + */ +@EqualsAndHashCode +@ToString +public class ImageHistory extends DockerObject implements Serializable { + + private static final long serialVersionUID = 1L; + + @JsonProperty("Id") + private String id; + + @JsonProperty("Created") + private Long created; + + @JsonProperty("CreatedBy") + private String createdBy; + + @JsonProperty("Tags") + private List tags; + + @JsonProperty("Size") + private Long size; + + @JsonProperty("Comment") + private String comment; + + @CheckForNull + public String getId() { + return id; + } + + public ImageHistory withId(String id) { + this.id = id; + return this; + } + + @CheckForNull + public Long getCreated() { + return created; + } + + public ImageHistory withCreated(Long created) { + this.created = created; + return this; + } + + @CheckForNull + public String getCreatedBy() { + return createdBy; + } + + public ImageHistory withCreatedBy(String createdBy) { + this.createdBy = createdBy; + return this; + } + + @CheckForNull + public List getTags() { + return tags; + } + + public ImageHistory withTags(List tags) { + this.tags = tags; + return this; + } + + @CheckForNull + public Long getSize() { + return size; + } + + public ImageHistory withSize(Long size) { + this.size = size; + return this; + } + + @CheckForNull + public String getComment() { + return comment; + } + + public ImageHistory withComment(String comment) { + this.comment = comment; + return this; + } +} diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/AbstractDockerCmdExecFactory.java b/docker-java-core/src/main/java/com/github/dockerjava/core/AbstractDockerCmdExecFactory.java index e04ab8e3e..5b908dff4 100644 --- a/docker-java-core/src/main/java/com/github/dockerjava/core/AbstractDockerCmdExecFactory.java +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/AbstractDockerCmdExecFactory.java @@ -29,6 +29,7 @@ import com.github.dockerjava.api.command.InspectContainerCmd; import com.github.dockerjava.api.command.InspectExecCmd; import com.github.dockerjava.api.command.InspectImageCmd; +import com.github.dockerjava.api.command.ImageHistoryCmd; import com.github.dockerjava.api.command.InspectNetworkCmd; import com.github.dockerjava.api.command.InspectServiceCmd; import com.github.dockerjava.api.command.InspectSwarmCmd; @@ -113,6 +114,7 @@ import com.github.dockerjava.core.exec.InspectContainerCmdExec; import com.github.dockerjava.core.exec.InspectExecCmdExec; import com.github.dockerjava.core.exec.InspectImageCmdExec; +import com.github.dockerjava.core.exec.ImageHistoryCmdExec; import com.github.dockerjava.core.exec.InspectNetworkCmdExec; import com.github.dockerjava.core.exec.InspectServiceCmdExec; import com.github.dockerjava.core.exec.InspectSwarmCmdExec; @@ -281,6 +283,11 @@ public InspectImageCmd.Exec createInspectImageCmdExec() { return new InspectImageCmdExec(getBaseResource(), getDockerClientConfig()); } + @Override + public ImageHistoryCmd.Exec createImageHistoryCmdExec() { + return new ImageHistoryCmdExec(getBaseResource(), getDockerClientConfig()); + } + @Override public ListContainersCmd.Exec createListContainersCmdExec() { return new ListContainersCmdExec(getBaseResource(), getDockerClientConfig()); diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/DockerClientImpl.java b/docker-java-core/src/main/java/com/github/dockerjava/core/DockerClientImpl.java index 55f530057..b17c1b481 100644 --- a/docker-java-core/src/main/java/com/github/dockerjava/core/DockerClientImpl.java +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/DockerClientImpl.java @@ -28,6 +28,7 @@ import com.github.dockerjava.api.command.InspectContainerCmd; import com.github.dockerjava.api.command.InspectExecCmd; import com.github.dockerjava.api.command.InspectImageCmd; +import com.github.dockerjava.api.command.ImageHistoryCmd; import com.github.dockerjava.api.command.InspectNetworkCmd; import com.github.dockerjava.api.command.InspectServiceCmd; import com.github.dockerjava.api.command.InspectSwarmCmd; @@ -113,6 +114,7 @@ import com.github.dockerjava.core.command.InspectContainerCmdImpl; import com.github.dockerjava.core.command.InspectExecCmdImpl; import com.github.dockerjava.core.command.InspectImageCmdImpl; +import com.github.dockerjava.core.command.ImageHistoryCmdImpl; import com.github.dockerjava.core.command.InspectServiceCmdImpl; import com.github.dockerjava.core.command.InspectSwarmCmdImpl; import com.github.dockerjava.core.command.InspectVolumeCmdImpl; @@ -375,6 +377,11 @@ public InspectImageCmd inspectImageCmd(String imageId) { return new InspectImageCmdImpl(getDockerCmdExecFactory().createInspectImageCmdExec(), imageId); } + @Override + public ImageHistoryCmd imageHistoryCmd(String imageId) { + return new ImageHistoryCmdImpl(getDockerCmdExecFactory().createImageHistoryCmdExec(), imageId); + } + /** * * CONTAINER API * */ diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/command/ImageHistoryCmdImpl.java b/docker-java-core/src/main/java/com/github/dockerjava/core/command/ImageHistoryCmdImpl.java new file mode 100644 index 000000000..fafbd8da1 --- /dev/null +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/command/ImageHistoryCmdImpl.java @@ -0,0 +1,42 @@ +package com.github.dockerjava.core.command; + +import java.util.List; +import java.util.Objects; + +import com.github.dockerjava.api.command.ImageHistoryCmd; +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.api.model.ImageHistory; + +/** + * Get the history of an image. + */ +public class ImageHistoryCmdImpl extends AbstrDockerCmd> implements + ImageHistoryCmd { + + private String imageId; + + public ImageHistoryCmdImpl(ImageHistoryCmd.Exec exec, String imageId) { + super(exec); + withImageId(imageId); + } + + @Override + public String getImageId() { + return imageId; + } + + @Override + public ImageHistoryCmd withImageId(String imageId) { + this.imageId = Objects.requireNonNull(imageId, "imageId was not specified"); + return this; + } + + /** + * @throws NotFoundException + * No such image + */ + @Override + public List exec() throws NotFoundException { + return super.exec(); + } +} diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/exec/ImageHistoryCmdExec.java b/docker-java-core/src/main/java/com/github/dockerjava/core/exec/ImageHistoryCmdExec.java new file mode 100644 index 000000000..8ba2a066d --- /dev/null +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/exec/ImageHistoryCmdExec.java @@ -0,0 +1,35 @@ +package com.github.dockerjava.core.exec; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.github.dockerjava.api.command.ImageHistoryCmd; +import com.github.dockerjava.api.model.ImageHistory; +import com.github.dockerjava.core.DockerClientConfig; +import com.github.dockerjava.core.MediaType; +import com.github.dockerjava.core.WebTarget; + +public class ImageHistoryCmdExec extends AbstrSyncDockerCmdExec> implements + ImageHistoryCmd.Exec { + + private static final Logger LOGGER = LoggerFactory.getLogger(ImageHistoryCmdExec.class); + + public ImageHistoryCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) { + super(baseResource, dockerClientConfig); + } + + @Override + protected List execute(ImageHistoryCmd command) { + WebTarget webResource = getBaseResource().path("/images/{id}/history").resolveTemplate("id", + command.getImageId()); + + LOGGER.trace("GET: {}", webResource); + + return webResource.request().accept(MediaType.APPLICATION_JSON).get(new TypeReference>() { + }); + } + +} diff --git a/docker-java/src/test/java/com/github/dockerjava/api/model/ImageHistoryTest.java b/docker-java/src/test/java/com/github/dockerjava/api/model/ImageHistoryTest.java new file mode 100644 index 000000000..8f18facf0 --- /dev/null +++ b/docker-java/src/test/java/com/github/dockerjava/api/model/ImageHistoryTest.java @@ -0,0 +1,90 @@ +package com.github.dockerjava.api.model; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.github.dockerjava.test.serdes.JSONSamples; +import com.github.dockerjava.test.serdes.JSONTestHelper; +import org.junit.Test; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_22; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +public class ImageHistoryTest { + + @Test + public void serderJson() throws IOException { + final List history = JSONTestHelper.getMapper().readValue( + JSONSamples.getSampleContent(VERSION_1_22, "images/history/history.json"), + new TypeReference>() { + } + ); + + assertThat(history, notNullValue()); + assertThat(history, hasSize(3)); + + final ImageHistory first = history.get(0); + assertThat(first.getId(), is("3db9c44f45209632d6050b35958829c3a2aa256d81b9a7be45b362ff85c54710")); + assertThat(first.getCreated(), is(1398108230L)); + assertThat(first.getCreatedBy(), is("/bin/sh -c #(nop) ADD file:eb15dbd63394e063b805a3c32ca7bf0266ef64676d5a6fab4801f2e81e2a5148 in /")); + assertThat(first.getTags(), hasSize(2)); + assertThat(first.getTags(), contains("ubuntu:lucid", "ubuntu:10.04")); + assertThat(first.getSize(), is(182964289L)); + assertThat(first.getComment(), is("")); + + final ImageHistory second = history.get(1); + assertThat(second.getId(), is("6cfa4d1f33fb861d4d114f43b25abd0ac737509268065cdfd69d544a59c85ab8")); + assertThat(second.getCreated(), is(1398108222L)); + assertThat(second.getTags(), empty()); + assertThat(second.getSize(), is(0L)); + + final ImageHistory third = history.get(2); + assertThat(third.getId(), is("511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158")); + assertThat(third.getCreated(), is(1371157430L)); + assertThat(third.getCreatedBy(), is("")); + assertThat(third.getTags(), contains("scratch12:latest", "scratch:latest")); + assertThat(third.getSize(), is(0L)); + assertThat(third.getComment(), is("Imported from -")); + + // Test round-trip serialization + final String serialized = JSONTestHelper.getMapper().writeValueAsString(history); + final List deserialized = JSONTestHelper.getMapper().readValue( + serialized, + new TypeReference>() { + } + ); + assertThat(deserialized, hasSize(3)); + assertThat(deserialized.get(0).getId(), is(first.getId())); + assertThat(deserialized.get(0).getCreated(), is(first.getCreated())); + assertThat(deserialized.get(0).getCreatedBy(), is(first.getCreatedBy())); + assertThat(deserialized.get(0).getTags(), is(first.getTags())); + assertThat(deserialized.get(0).getSize(), is(first.getSize())); + assertThat(deserialized.get(0).getComment(), is(first.getComment())); + } + + @Test + public void builderPattern() { + final ImageHistory history = new ImageHistory() + .withId("abc123") + .withCreated(1234567890L) + .withCreatedBy("/bin/sh -c echo hello") + .withTags(Arrays.asList("myimage:latest")) + .withSize(1024L) + .withComment("test comment"); + + assertThat(history.getId(), is("abc123")); + assertThat(history.getCreated(), is(1234567890L)); + assertThat(history.getCreatedBy(), is("/bin/sh -c echo hello")); + assertThat(history.getTags(), contains("myimage:latest")); + assertThat(history.getSize(), is(1024L)); + assertThat(history.getComment(), is("test comment")); + } +} diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/ImageHistoryCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/ImageHistoryCmdIT.java new file mode 100644 index 000000000..16ae7e6a2 --- /dev/null +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/ImageHistoryCmdIT.java @@ -0,0 +1,29 @@ +package com.github.dockerjava.cmd; + +import com.github.dockerjava.api.model.ImageHistory; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.notNullValue; + +public class ImageHistoryCmdIT extends CmdIT { + + @Test + public void imageHistory() { + List history = dockerRule.getClient().imageHistoryCmd("busybox").exec(); + + assertThat(history, notNullValue()); + assertThat(history, hasSize(greaterThan(0))); + + ImageHistory entry = history.get(0); + assertThat(entry.getId(), notNullValue()); + assertThat(entry.getCreated(), notNullValue()); + assertThat(entry.getCreatedBy(), notNullValue()); + assertThat(entry.getSize(), notNullValue()); + assertThat(entry.getComment(), notNullValue()); + } +} diff --git a/docker-java/src/test/resources/samples/1.22/images/history/history.json b/docker-java/src/test/resources/samples/1.22/images/history/history.json new file mode 100644 index 000000000..a38da2d8f --- /dev/null +++ b/docker-java/src/test/resources/samples/1.22/images/history/history.json @@ -0,0 +1,32 @@ +[ + { + "Id": "3db9c44f45209632d6050b35958829c3a2aa256d81b9a7be45b362ff85c54710", + "Created": 1398108230, + "CreatedBy": "/bin/sh -c #(nop) ADD file:eb15dbd63394e063b805a3c32ca7bf0266ef64676d5a6fab4801f2e81e2a5148 in /", + "Tags": [ + "ubuntu:lucid", + "ubuntu:10.04" + ], + "Size": 182964289, + "Comment": "" + }, + { + "Id": "6cfa4d1f33fb861d4d114f43b25abd0ac737509268065cdfd69d544a59c85ab8", + "Created": 1398108222, + "CreatedBy": "/bin/sh -c #(nop) MAINTAINER Tianon Gravi - mkimage-debootstrap.sh -i iproute,iputils-ping,ubuntu-minimal -t lucid.tar.xz lucid http://archive.ubuntu.com/ubuntu/", + "Tags": [], + "Size": 0, + "Comment": "" + }, + { + "Id": "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158", + "Created": 1371157430, + "CreatedBy": "", + "Tags": [ + "scratch12:latest", + "scratch:latest" + ], + "Size": 0, + "Comment": "Imported from -" + } +]