我正在尝试使用Spotify Docker 客户端在 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)
...
由于堆栈跟踪提到的一些奇怪之处,unix://localhost:80
实际上它实际上是在使用套接字 url unix:///var/run/docker.sock
。我已经通过调试器逐步确认了这一点。
否则 Docker 工作得很好;我可以通过“curl”连接到套接字,并且可以通过终端正常使用 docker。