Use the docker client to perform lifecycle operations on images, including listing, building, pulling, pushing, tagging, and removing them.
- List images: Use
listImages with ListImagesParam to filter by labels. - Build images: Use
build with a path to a Dockerfile and a ProgressHandler to capture the resulting image ID. - Create images: Create images by
pulling from a registry (using RegistryAuth) or by createing from an InputStream (loading a tarball). - Inspect/History: Use
inspectImage for metadata and history for the image's layer history. - Tagging: Use
tag to assign names. The method supports a force boolean to re-assign tags. - Push/Remove: Use
push to upload to a registry and removeImage to delete locally.
// List images with labels
final List<Image> quxImages = docker.listImages(ListImagesParam.withLabel("foo", "qux"));
// Build an image
final String returnedImageId = docker.build(
Paths.get(dockerDirectory), "test", new ProgressHandler() {
@Override
public void progress(ProgressMessage message) throws DockerException {
final String imageId = message.buildImageId();
// handle imageId
}
});
// Pull an image with authentication
final RegistryAuth registryAuth = RegistryAuth.builder()
.email(AUTH_EMAIL)
.username(AUTH_USERNAME)
.password(AUTH_PASSWORD)
.build();
docker.pull("dxia2/scratch-private:latest", registryAuth);
// Tag an image
docker.tag("busybox:latest", "testRepo/tagForce:sometag");
// Force-re-assign tag
docker.tag("busybox:buildroot-2014.02", "testRepo/tagForce:sometag", true);