0
votes

I 'am new to docker , I want to launch docker containers using java client ,

I tried https://github.com/docker-java/docker-java as below

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();
    }   
}

with setting the required configurations in 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"

Docker Images in localhost

dockerimages

I have got this Exception

javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:2375 [/127.0.0.1] failed: Connection refused (Connection refused) at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:481) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681) at 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) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343) at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:29) at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:12) at com.github.dockerjava.jaxrs.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:23) at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35) at com.github.dockerjava.core.command.StartContainerCmdImpl.exec(StartContainerCmdImpl.java:46) at containers.launch_containers.StartContainers.startContainers(StartContainers.java:18) at containers.launch_containers.StartContainers.(StartContainers.java:10) at containers.launch_containers.StartContainers.main(StartContainers.java:29)

1

1 Answers

0
votes

I solved this issue , I should get Container Id as below

   dockerClient = DockerClientBuilder.getInstance().build();
    //starting the container    
   CreateContainerResponse container = dockerClient.createContainerCmd("hello-world")
           .withCmd()
           .exec();
   dockerClient.startContainerCmd(container.getId()).exec();
   //stop the container 
   dockerClient.stopContainerCmd(container.getId()).exec();

reference

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