I am attempting to automatically manage my application in a Docker container using the Spotify Docker Client for my integration tests with 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);
the createContainer
call fails:
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, where the stacktrace mentions unix://localhost:80
, under the hood it is actually using the socket url unix:///var/run/docker.sock
. I have confirmed this by stepping through with the debugger.
Docker works perfectly fine otherwise; I can connect to the socket via 'curl' and I can use docker via the terminal normally.