gpt4 book ai didi

java - Docker-java API 在 Windows 10 上的 Eclipse 中连接 virtualbox ubuntu docker

转载 作者:行者123 更新时间:2023-12-02 12:25:36 25 4
gpt4 key购买 nike

我已经在Windows 10上安装了virtualbox,并为docker主机创建了一个Ubuntu实例。然后在Ubuntu上安装docker。我想使用docker-java API连接docker(我使用的是docker-java 3.0.12),代码如下:

DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://192.168.5.31:2375").build();
DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();

CreateContainerResponse container1 =
dockerClient.createContainerCmd("tomcat:latest").withCmd("sleep", "9999") .withName("mytomcat3").exec();

dockerClient = DockerClientBuilder.getInstance().build();
dockerClient.startContainerCmd(container1.getId()).exec();

我得到以下信息:

java.lang.RuntimeException: Library 'junixsocket-native-2.0.4.dll' not found!
at org.newsclub.net.unix.NarSystem.getLibPath(NarSystem.java:132)
at org.newsclub.net.unix.NarSystem.loadLibrary(NarSystem.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.newsclub.net.unix.NativeUnixSocket.<clinit>(NativeUnixSocket.java:35)
at org.newsclub.net.unix.AFUNIXSocket.<init>(AFUNIXSocket.java:36)
at org.newsclub.net.unix.AFUNIXSocket.newInstance(AFUNIXSocket.java:54)
at com.github.dockerjava.jaxrs.ApacheUnixSocket.<init>(ApacheUnixSocket.java:51)
at com.github.dockerjava.jaxrs.UnixConnectionSocketFactory.createSocket(UnixConnectionSocketFactory.java:66)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:118)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:435)
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 docker.MyDocker.main(MyDocker.java:53)...

我无法下载该 dll。您能告诉我如何解决这个问题吗?

非常感谢!

最佳答案

我找到了使用另一个 Java API 的解决方案。即spotify/docker-client 。我使用这样的 API:

import java.net.URI;
import java.util.Arrays;

import com.google.common.collect.ImmutableMap;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ExecCreation;
import com.spotify.docker.client.messages.HostConfig;
import com.spotify.docker.client.messages.HostConfig.Bind;
import com.spotify.docker.client.messages.PortBinding;

public class MyDocker {

public static void main(String[] args) {
try {
DockerClient dockerClient = DefaultDockerClient.builder()
.uri(URI.create("http://192.168.5.31:2375"))
.build();
// Pull an image
//docker.pull("tomcat");

String p = PortBinding.randomPort("192.168.5.31").hostPort();

final HostConfig hostConfig = HostConfig.builder()
.appendBinds(Bind.from("/home/cdh/test")
.to("/usr/local/tomcat/webapps/test").readOnly(false)
.build())
.portBindings(ImmutableMap.of("8080/tcp", Arrays.asList(PortBinding.of("0.0.0.0", p)))).build();



final ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.image("tomcat:latest").exposedPorts("8080/tcp")
.build();

final ContainerCreation creation = dockerClient.createContainer(containerConfig);
final String id = creation.id();

// Start container
dockerClient.startContainer(id);
// Exec command inside running container with attached STDOUT and STDERR
final String[] command = {"bash", "-c", "ls"};
final ExecCreation execCreation = dockerClient.execCreate(
id, command, DockerClient.ExecCreateParam.attachStdout(),
DockerClient.ExecCreateParam.attachStderr());
final LogStream output = dockerClient.execStart(execCreation.id());
final String execOutput = output.readFully();

// Kill container
dockerClient.killContainer(id);

// Remove container
dockerClient.removeContainer(id);

// Close the docker client
dockerClient.close();

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

它工作得很好。现在我建议你可以使用下面的代码来访问docker。

干杯!

关于java - Docker-java API 在 Windows 10 上的 Eclipse 中连接 virtualbox ubuntu docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45496205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com