Docker API Client with Spring Boot

Humayoun Kabir Emon
4 min readMay 13, 2020
Docker API with Spring Boot

In this article, we will connect with a running Docker daemon and perform some operation using Docker official API from our Spring Boot application. For this purpose, we will use popular API: Java Docker API Client.

Before continuing, we have to make sure that, Docker expose daemon is checked.

First, we need to add one dependency to our pom.xml file. We can get latest version from Maven repository and Github release page.

<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.2.1</version>
</dependency>

We can establish a connection between Docker daemon and Spring boot project DockerClient interface.

Now, we will create a Spring Bean of it and this Bean can be created by several ways.

By specifying default configuration:

@Bean
DockerClient dockerClient() {
return DockerClientBuilder.getInstance().build();
}

We can also create DockerClient by providing IP address and Port number.

@Bean
DockerClient dockerClient() {
return DockerClientBuilder.getInstance(
DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://localhost:2375")
.build())
.build();
}

Here, we have to provide unix:// or tcp:// depending on the connection type.

Now, we are successful to establish connection between Docker daemon and our Spring boot application.

Docker Image:

Here, Some useful and regularly used commands of Docker image are explained.

Image List:

We can fetch Docker image list using this command.

List<Image> imageList = dockerClient.listImagesCmd().exec();

In case, we need to get the intermediate images, we need to request it explicitly:

List<Image> imageList = dockerClient.listImagesCmd()
.withShowAll(true)
.exec();

Image Pull:

To pull image from repository, we have to provide repository name and tag name is optional.

boolean bool = dockerClient.pullImageCmd("repository")
.withTag("image_tag")
.exec(new PullImageResultCallback())
.awaitCompletion(30, TimeUnit.SECONDS);

Here, awaitCompletion waits until full image is fetched from Docker hub.

Image Build:

We build Docker image by specifying the path of Dockerfile. If everything goes well, we should eventually see the image with the given tag name.

String image = dockerClient.buildImageCmd()
.withDockerfile(new File("path/to/Dockerfile"))
.withTag("tag_name")
.exec(new BuildImageResultCallback())
.awaitImageId();

Image Inspect:

We can get the inspect of a Docker image using inspectImageCmd method.

InspectImageResponse inspectImage = dockerClient
.inspectImageCmd("image_id")
.exec();

Image Push:

To comply with the service, the Docker client must be configured, as operating with registries must be authenticated in advance.

boolean bool = dockerClient
.pushImageCmd("docker_hub_repository")
.withTag("tag_name")
.exec(new PushImageResultCallback())
.awaitCompletion(30, TimeUnit.SECONDS);

Image Remove:

To remove image by simple using removeImageCmd method.

dockerClient.removeImageCmd("image_id").exec();

Docker Container:

Here, Some useful and regularly used commands of Docker container are explained.

Container List:

We can fetch Docker container list using this command.

List<Container> containerList = dockerClient
.listContainersCmd()
.exec();

This only returns running container. In case, if we want display containers with the certain status, then we have to provide explicitly.

dockerClient.listContainersCmd()
.withShowAll(true)
.withShowSize(true)
.withStatusFilter(Collections.singleton("container_status"))
.exec();

Container Create:

To create container, we need to call createContainerCmd method and has to provide the image name. This will return CreateContainerResponse.

CreateContainerResponse containerResponse = dockerClient
.createContainerCmd("image_name")
.exec();

This method does not expose any port for this container. If we want it, then we have to provide Exposed Port with withExposedPorts method.

ExposedPort tcp01 = ExposedPort.tcp(8080);
ExposedPort tcp02 = ExposedPort.tcp(8080);

CreateContainerResponse containerResponse = dockerClient
.createContainerCmd("image_name")
.withName("tag_name")
.withHostName("ip_address")
.withExposedPorts(tcp01, tcp02)
.exec();

Note: This does not start that container. It just creates, we have to start it by own.

Container Start, Stop, Kill, Restart:

We can start, stop, kill and restart container based on our requirement.

// Start Container
dockerClient.startContainerCmd(containerResponse.getId()).exec();

// Stop Container
dockerClient.stopContainerCmd(containerResponse.getId()).exec();

// Kill Container
dockerClient.killContainerCmd(containerResponse.getId()).exec();

// Restart Container
dockerClient.restartContainerCmd(containerResponse.getId()).exec();

Container Inspect:

To inspect container we need to call inspectContainerCmd method.

InspectContainerResponse inspectContainerResponse = dockerClient
.inspectContainerCmd(containerResponse.getId())
.exec();

Docker Volume:

Here, Some useful and regularly used commands of Docker volume are explained.

Volume List:

All of the available volumes are listed with:

List<InspectVolumeResponse> volumeResponseList = dockerClient
.listVolumesCmd()
.exec()
.getVolumes();

This includes named and unnamed all volumes.

Volume Create:

We can create volume with createVolumeCmd method and can give a name using withName.

CreateVolumeResponse volumeName = dockerClient
.createVolumeCmd().withName("volume_name")
.exec();

Volume Remove:

The removeVolumeCmd method is used for removing volume.

dockerClient.removeVolumeCmd("volume_name")
.exec();

Note: We cannot delete a volume if it is in use from a container.

Volume Inspect:

The inspectVolumeCmd method is the form to show the detailed information of a volume.

InspectVolumeResponse inspectVolume = dockerClient
.inspectVolumeCmd("volume_id")
.exec();

In this tutorial, we discussed about Java Docker API Client using Spring boot application on Docker Engine along with several implementation approaches for deployment scenarios.

--

--