在windows 10上的Eclipse中的插件-java API连接虚拟盒子ubuntu插件

yqkkidmi  于 2022-11-02  发布在  Windows
关注(0)|答案(2)|浏览(161)

我已经在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。你能告诉我如何解决这个问题吗?
非常感谢!

sz81bmfz

sz81bmfz1#

我已经找到了一个使用另一个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。
干杯干杯干杯

toe95027

toe950272#

junixsocket版本2.0.4不支持Windows。您需要升级到更高版本。
尝试将junixsocket依赖项更改为

<dependency>
  <groupId>com.kohlschutter.junixsocket</groupId>
  <artifactId>junixsocket-core</artifactId>
  <version>2.5.2</version>
  <type>pom</type>
</dependency>

相关问题