0

I am using the docker-java Maven library, and I would like to know if there is a way to check if an image has already been pulled or is present locally before pulling it if necessary, with only the image name. Currently, I have to pull the image everytime I execute it to ensure it is present locally, which is not nice.

Ordinaly
  • 69
  • 1
  • 9

1 Answers1

0

If you need to do it in Java, you can use the inspectImageCmd method of the com.github.dockerjava.api.DockerClient interface. Then check the returned InspectImageResponse response object. Something like this:

String imageId = ...;
InspectImageResponse response = dockerClient.inspectImageCmd(imageid).exec();

docker-java is just a wrapper for the Docker REST API and the inspect command is the equivalent of the GET http request to /images/{imageid}/json. See section 3.2 Images in Docker Engine API.

b0gusb
  • 4,283
  • 2
  • 14
  • 33
  • With this method I have to catch an exception: com.github.dockerjava.api.exception.NotFoundException which is not very clean imo but it might be the only way... – Ordinaly Jul 10 '19 at 06:29
  • I don't like it either but that's how the library works. The REST call returns a `404 (Not Found)` HTTP response if the image does not exist which is translated into a `NotFoundException`. – b0gusb Jul 10 '19 at 07:11
  • Glad it helped. – b0gusb Jul 10 '19 at 07:40