Categories
Mastering Development

How to connect to docker.sock using Netty?

I have a Reactive Springboot application using Spring WebFlux. I am 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 am getting 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?

Update: 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.

Update 2: 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

Update 3: This makes no sense, but I think I got it working. I put in a resolvable address like google.com and im getting expected results from the docker socket. I don’t understand why this would be required at all.

return client.get()
        .uri("http://google.com/v1.40/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();
            }
        });

Leave a Reply

Your email address will not be published. Required fields are marked *