Can the Docker Java client library (https://github.com/docker-java/docker-java) be used to execute custom commands?
I want to use the library to check if Docker images I need to download are compatible with multiple architectures (specifically, linux/arm/v6 and linux/amd64). If the images meet this criteria, I would like to pull them and then push them to a different registry, renaming them based on their architecture one by one.
For example: I pull the alpine:latest image which supports both linux/arm/v6 and linux/amd64 architectures, then I rename it to alpine-amd64:latest, alpine-armv6:latest, then I push this to my private registry.
I feel like I'm constrained by Docker Java client.
public void pullAndPushDockerImage(DockerImageRestRequest request, String appId) {
String imageName = "alpine";
String imageTag = "latest";
List<String> platforms = List.of("linux/amd64", "linux/arm/v6");
try (DockerClient client = createDockerClient(request, dockerHost)) {
authConfig = Optional.ofNullable(new AuthConfig()
.withUsername(request.getUserName())
.withIdentityToken(request.getAccessToken())
.withRegistryAddress(request.getExternalRegistryUrl()));
for (String platform : platforms) {
client.pullImageCmd(imageName + ":" + imageTag)
.withPlatform(platform)
.exec(new PullImageResultCallback() {
@Override
public void onNext(PullResponseItem item) {
// Verify image pull status
String status = item.getStatus();
System.out.println(status);
}
}).awaitCompletion();
}
...
// pushing the images later on
}
}
Logs:
Pulling from library/alpine
Digest: sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11
Status: Downloaded newer image for alpine:latest
Pulling from library/alpine
Digest: sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11
Status: Downloaded newer image for alpine:latest
pom.xml dependencies:
<!-- Docker Client for Java -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.2.13</version>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>3.2.13</version>
</dependency>
I tried the built-in pullImageCmd with the withPlatform parameter
If you have to do this with your docker command line you will first save the image with an other name then push it to your repository. So i suggest you to use a save command to create the new image name then reuse the image result as your parameter to push the image.