使用Java客户端启动docker容器时连接被拒绝

时间:2017-05-03 11:14:57

标签: java docker

我是docker的新手,我想使用java客户端启动docker容器,

我尝试https://github.com/docker-java/docker-java如下

package containers.launch_containers;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientBuilder;

public class StartContainers{
     DockerClient dockerClient;

    public StartContainers() {
        startContainers();
    }

    public void startContainers() {
           try {

                dockerClient = DockerClientBuilder.getInstance("tcp://127.0.0.1:2375").build();
                //starting the container of hello-world 
                dockerClient.startContainerCmd("hello-world").exec();


    }catch(Exception ex)
           {
             ex.printStackTrace();
           }
    }

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

在bashrc中设置所需的配置

  

export DOCKER_HOST = tcp://127.0.0.1:2375

     

export DOCKER_OPTS =" -H tcp://127.0.0.1:2375 -H   UNIX:///var/run/docker.sock"

localhost中的Docker镜像

dockerimages

我有这个例外

  

javax.ws.rs.ProcessingException:   org.apache.http.conn.HttpHostConnectException:连接到   127.0.0.1:2375 [/127.0.0.1]失败:连接被拒绝(连接被拒绝)at   org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:481)     在   org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)     在   org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:684)     在   org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:681)     在org.glassfish.jersey.internal.Errors.process(Errors.java:315)at   org.glassfish.jersey.internal.Errors.process(Errors.java:297)at   org.glassfish.jersey.internal.Errors.process(Errors.java:228)at   org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)     在   org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)     在   org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:437)     在   org.glassfish.jersey.client.JerseyInvocation $ Builder.post(JerseyInvocation.java:343)     在   com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:29)     在   com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:12)     在   com.github.dockerjava.jaxrs.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:23)     在   com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35)     在   com.github.dockerjava.core.command.StartContainerCmdImpl.exec(StartContainerCmdImpl.java:46)     在   containers.launch_containers.StartContainers.startContainers(StartContainers.java:18)     在   containers.launch_containers.StartContainers。(StartContainers.java:10)     在   containers.launch_containers.StartContainers.main(StartContainers.java:29)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我应该得到如下的容器ID

Function AddToArray(ys) As String()
Dim a() As String
Dim size As Integer
Dim i As Integer
Dim sh As Worksheet
Dim rw As Range
size = 0
ReDim Preserve a(size)
For Each rw In sh.Rows
    If Match(sh.Cells(rw.Row, 1), s, column) = True Then
        ??

size = size + 1
End Function

参考

https://github.com/docker-java/docker-java/wiki