Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all containers/images using docker-java client library [closed]

Tags:

java

docker

I have imported docker-java client library.

Using above library how can I get all containers/images available?

Sample code will help a lot.

like image 659
rohitkadam19 Avatar asked Feb 09 '23 14:02

rohitkadam19


1 Answers

Able to get containers/images using docker-java client library.

To get all the images

 DockerClient dockerClient = DockerClientBuilder.getInstance("http://localhost:2375").build();
 List<Image> images = dockerClient.listImagesCmd().exec();

    for(int i=0; i < images.size(); i++){
        System.out.println(images.get(i));
    }

To get all the containers

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder;

String localDockerHost = SystemUtils.IS_OS_WINDOWS ? "tcp://localhost:2375" : "unix:///var/run/docker.sock";
DockerClient dockerClient = DockerClientBuilder.getInstance(localDockerHost).build();
List<Container> containers = dockerClient.listContainersCmd().exec();

    for(int i=0; i < containers.size(); i++){
        System.out.println(containers.get(i));
    }
like image 176
rohitkadam19 Avatar answered Feb 12 '23 06:02

rohitkadam19