我是Docker技术的初学者,我一直在尝试构建一个能够启动和停止现有Docker容器的Java应用程序。
为此我一直在使用Java Docker API客户端 - docker-java。 https://github.com/docker-java/docker-java我一直在使用https://github.com/docker-java/docker-java/wiki测试用例来观察此API的工作原理,因为我无法找到任何相关的教程。
使用代码,我已经使用以下代码示例启用了Java应用程序来创建Docker容器。
CreateContainerResponse container = client.createContainerCmd("chirangaalwis/docker-whale")
.withCmd("/bin/sh -c '/usr/gam")
.exec();
但是我没有成功启动我使用Java应用程序创建的容器,它抛出了以下异常:
Exception in thread "main" com.github.dockerjava.api.InternalServerErrorException: Cannot start container 485386a92b34c5aaf53793207e5c6a8146295b23602c9d0adf86d64a1b27d031: write /sys/fs/cgroup/cpuset/docker/485386a92b34c5aaf53793207e5c6a8146295b23602c9d0adf86d64a1b27d031/cpuset.cpus: invalid argument
at com.github.dockerjava.core.util.ResponseStatusExceptionFilter.filter(ResponseStatusExceptionFilter.java:53)
at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:139)
at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:127)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:171)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:259)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:671)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:668)
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:668)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:428)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:334)
at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:28)
at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:13)
at com.github.dockerjava.jaxrs.AbstrDockerCmdExec.exec(AbstrDockerCmdExec.java:53)
at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:29)
at com.github.dockerjava.core.command.StartContainerCmdImpl.exec(StartContainerCmdImpl.java:53)
at lk.ac.iit.java_docker.DockerWithJavaClass.main(DockerWithJavaClass.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
这是我用于上述目的的代码示例:
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.SearchItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.kpelykh.docker.client.model.ContainerCreateResponse;
import java.util.List;
/**
* Created by chiranga on 7/29/15.
*/
public class DockerWithJavaClass {
public static void main(String[] args) {
DockerClient client = DockerClientBuilder.getInstance("http://localhost:2375").build();
System.out.println("This is my client " + client);
System.out.println();
Info info = client.infoCmd().exec();
System.out.println(info);
System.out.println();
CreateContainerResponse container = client.createContainerCmd("chirangaalwis/docker-whale")
.withCmd("/bin/sh -c '/usr/gam")
.exec();
client.startContainerCmd(container.getId()).exec();
}
}
基于异常消息,我更改了代码,使得id与给定的完整路径一起打印,这导致了另一个新的异常,如下所示:
Exception in thread "main" com.github.dockerjava.api.DockerException
at com.github.dockerjava.core.util.ResponseStatusExceptionFilter.filter(ResponseStatusExceptionFilter.java:55)
at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:139)
at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:127)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:171)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:259)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:671)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:668)
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:668)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:428)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:334)
at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:28)
at com.github.dockerjava.jaxrs.StartContainerCmdExec.execute(StartContainerCmdExec.java:13)
at com.github.dockerjava.jaxrs.AbstrDockerCmdExec.exec(AbstrDockerCmdExec.java:53)
at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:29)
at com.github.dockerjava.core.command.StartContainerCmdImpl.exec(StartContainerCmdImpl.java:53)
at lk.ac.iit.java_docker.DockerWithJavaClass.main(DockerWithJavaClass.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
在我获得上述新异常之前,对代码进行了以下更改:
client.startContainerCmd("/sys/fs/cgroup/cpuset/docker/" + container.getId() + "/cpuset.cpus").exec();
我对这项技术完全陌生,如果有人能为我提供解决这个问题的解决方案,并且如果有更好的替代方案来实现这项任务,我将不胜感激,请具体说明。