列出docker images

时间:2017-05-01 19:10:38

标签: java docker containers devops

我试图通过https://github.com/docker-java/docker-java

使用java client api列出docker镜像

这是我的代码段

import java.util.List;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder;
    public class StartContainers{
        DockerClient dockerClient ;
        public StartContainers() {
            startContainers();
        }

    public void startContainers() {

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

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

    }

    public static void main(String args[])
    {
        StartContainers startContainers=new StartContainers();
    }
}

我得到了这个例外

  

线程“main”中的异常java.lang.ExceptionInInitializerError at   org.glassfish.jersey.client.JerseyWebTarget。(JerseyWebTarget.java:71)     在   org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:290)     在   org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:76)     在   com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory.init(JerseyDockerCmdExecFactory.java:234)     在   com.github.dockerjava.core.DockerClientImpl.withDockerCmdExecFactory(DockerClientImpl.java:161)     在   com.github.dockerjava.core.DockerClientBuilder.build(DockerClientBuilder.java:47)     在   com.cit.security.controllers.StartContainers.startContainers(StartContainers.java:18)     在   com.cit.security.controllers.StartContainers。(StartContainers.java:13)     在   com.cit.security.controllers.StartContainers.main(StartContainers.java:29)   引起:java.lang.RuntimeException:无法创建jax-rs   RuntimeDelegate at   javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:149)     在javax.ws.rs.core.UriBuilder。(UriBuilder.java:56)

1 个答案:

答案 0 :(得分:0)

您能描述运行该代码的环境吗? 我设法在Debian主机上成功运行该代码(作为maven项目),以root(sudo nohup docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock &)运行docker守护程序并启动虚拟容器。