Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to docker.sock using Netty?

I have a Reactive Springboot application using Spring WebFlux. I'm trying to connect to my /var/run/docker.sock Unix Domain Socket to query some information.

From my terminal, I am able to fetch all running containers using the following command.

curl --unix-socket /var/run/docker.sock http:/v1.40/containers/json

I am following the Project Reactor guide, found here to create an HttpClient for Unix Domain Sockets

My test code is as follows.

return client.get()
        .uri("/containers/json")
        .responseContent()
        .asString()
        .collectList()
        .flatMapMany(new Function<List<String>, Publisher<? extends Container>>() {
            @Override
            public Publisher<? extends Container> apply(List<String> strings) {
                return Flux.empty();
            }
        });

The HttpClient is created like this.

private HttpClient getOperationsClient(OperationsProperties properties) {
    return HttpClient.create()
            .remoteAddress(() -> new DomainSocketAddress("/var/run/docker.sock"));
}

When making a request, I get the following error.

io.netty.channel.AbstractChannel$AnnotatedConnectException: connect(..) failed: Invalid argument: /var/run/docker.sock

Digging into the error a bit, the error is coming from the following line within io.netty.channel.unix.Socket.java (line 230)

res = connectDomainSocket(fd, unixDomainSocketAddress.path().getBytes(CharsetUtil.UTF_8));

res is getting a value back of -22, which translates to the "Invalid Argument" in the stacktrace. I have verified that my user is correct, and has the proper 'rw' permissions to /var/run/docker.sock (my user is in the docker group).

What am I doing wrong?

I tried changing the paths around. I tried a socket address of unix:///var/run/docker.sock, which results in Address family not supported by protocol. I also tried changing the DomainSocket URI to something that doesn't exist such as /var/run/test.sock which also results in -22 Invalid Argument.

I added the jvm argument -Djava.net.preferIPv4Stack=true. It still doesn't work but it seems like the original -22 error has gone away. I am now facing -97 Address family not supported by protocol: /var/run/docker.sock

like image 786
Nick H Avatar asked Oct 15 '20 14:10

Nick H


1 Answers

Well, turns out Unix Domain Support is only available in the snapshot 1.0.0-RC2. Configuring spring to use the 2.4.0-SNAPSHOT transitively pulled in the supported io.projectreactor module which had the Unix Domain Socket support. It is now working as expected.

like image 169
Nick H Avatar answered Sep 24 '22 10:09

Nick H