英文:
How to connect to docker.sock using Netty?
问题
我有一个使用Spring WebFlux的响应式Spring Boot应用程序。我正在尝试连接到我的/var/run/docker.sock Unix域套接字以查询一些信息。
从我的终端,我能够使用以下命令获取所有正在运行的容器。
curl --unix-socket /var/run/docker.sock http:/v1.40/containers/json
我正在遵循Project Reactor指南,在这里找到创建用于Unix域套接字的HttpClient的方法。
我的测试代码如下。
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();
}
});
HttpClient是这样创建的。
private HttpClient getOperationsClient(OperationsProperties properties) {
return HttpClient.create()
.remoteAddress(() -> new DomainSocketAddress("/var/run/docker.sock"));
}
在发出请求时,我得到以下错误。
io.netty.channel.AbstractChannel$AnnotatedConnectException: connect(..) failed: Invalid argument: /var/run/docker.sock
稍微深入研究了一下错误,错误来自io.netty.channel.unix.Socket.java中的以下行(第230行)
res = connectDomainSocket(fd, unixDomainSocketAddress.path().getBytes(CharsetUtil.UTF_8));
res
返回的值是-22,这在堆栈跟踪中对应着"Invalid Argument"。我已经验证过我的用户是正确的,并且对/var/run/docker.sock具有适当的'rw'权限(我的用户在docker组中)。
我做错了什么?
我尝试过调整路径。我尝试过socket地址为unix:///var/run/docker.sock
,结果是Address family not supported by protocol
。
我还尝试过将DomainSocket URI更改为不存在的内容,例如/var/run/test.sock
,这也会导致-22 Invalid Argument
。
我添加了jvm参数-Djava.net.preferIPv4Stack=true
。它仍然不起作用,但似乎原始的-22错误已经消失。我现在面临的是-97 Address family not supported by protocol: /var/run/docker.sock
。
英文:
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
答案1
得分: 5
好的,以下是翻译好的部分:
好吧,事实证明Unix域支持仅在快照版本1.0.0-RC2中可用。配置Spring使用2.4.0-SNAPSHOT在传递中引入了受支持的io.projectreactor模块,其中包含Unix域套接字支持。现在已经按预期工作。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论