例如:查询正在运行的容器列表,HTTP 方式如下:
$ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json [{ "Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772", "Names":["/tender_wing"], "Image":"bfirsh/reticulate-splines", ... }]复制代码
在本机执行如下命令
curl -v --unix-socket /var/run/docker.sock http:/v1.24/containers/json 复制代码
1、引入 UnixSocket 工具包
<dependency> <groupId>com.github.jnr</groupId> <artifactId>jnr-unixsocket</artifactId> <version>0.18</version> </dependency>复制代码
2、测试代码
public static void main(String[] args) { // 建立 Unix Socket 连接 File sockFile = new File("/var/run/docker.sock"); UnixSocketAddress address = new UnixSocketAddress(sockFile); UnixSocketChannel channel = UnixSocketChannel.open(address); UnixSocket unixSocket = new UnixSocket(channel); // 调用 Docker API PrintWriter w = new PrintWriter(unixSocket.getOutputStream()); w.println("GET /v1.24/containers/json HTTP/1.1"); w.println("Host: http"); w.println("Accept: */*"); w.println(""); w.flush(); // 关闭 Output,否则会导致下面的 read 操作一直阻塞 unixSocket.shutdownOutput(); // 获取返回结果 System.out.println("---- Docker Response ----"); BufferedReader br = new BufferedReader(new InputStreamReader(unixSocket.getInputStream())); String line; while ((line = br.readLine()) != null){ System.out.println(line); } unixSocket.close(); }复制代码
© 著作权归作者所有,转载或内容合作请联系作者
● Service Mesh - gRPC 本地联调远程服务
● 使用 Thymeleaf 动态渲染 HTML
● Fastjson致命缺陷
● Spring Boot 2 集成log4j2日志框架
● Java面试通关要点汇总集之核心篇参考答案
● Java面试通关要点汇总集之框架篇参考答案
● Spring Security 实战干货:如何保护用户密码
● Spring Boot RabbitMQ - 优先级队列
本文由博客一文多发平台 OpenWrite 发布!