我正在尝试使用Spotify Docker Client在Docker容器中使用Docker for Mac自动管理我的应用程序。
DockerClient docker = new DefaultDockerClient.fromEnv().build();
HostConfig hostConfig = HostConfig.builder()
.portBindings(new HashMap<String, List<PortBinding>>(){{
put("8080", Arrays.asList(PortBinding.of("localhost", 8080)));
}}).build();
ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.exposedPorts("8080")
.image("my-app")
.portSpecs(Arrays.asList("8080:8080"))
.build();
ContainerCreation createdContainer = docker.createContainer(containerConfig);
createContainer
调用失败:
2091 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->unix://localhost:80][total kept alive: 0; route allocated: 0 of 100; total allocated: 0 of 100]
2091 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 1][route: {}->unix://localhost:80][total kept alive: 0; route allocated: 1 of 100; total allocated: 1 of 100]
2091 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->unix://localhost:80
2456 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/127.0.0.1:80
2476 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-1: Shutdown connection
2516 [jersey-client-async-executor-0] DEBUG org.apache.http.impl.execchain.MainClientExec - Socket is not connected
java.io.IOException: Socket is not connected
at jnr.enxio.channels.NativeSocketChannel.shutdownInput(NativeSocketChannel.java:102)
at com.spotify.docker.client.ApacheUnixSocket.shutdownInput(ApacheUnixSocket.java:280)
at com.spotify.docker.client.ApacheUnixSocket.close(ApacheUnixSocket.java:273)
...
Due to some weirdness,其中stacktrace提到unix://localhost:80
,它实际上是使用套接字网址unix:///var/run/docker.sock
。我通过调试器逐步证实了这一点。
Docker的工作方式非常好;我可以通过&#39; curl&#39;连接到插座。我可以通过终端正常使用docker。