例如:查詢正在運行的容器列表,HTTP 方式以下:git
$ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json
[{
"Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772",
"Names":["/tender_wing"],
"Image":"bfirsh/reticulate-splines",
...
}]複製代碼
在本機執行以下命令github
curl -v --unix-socket /var/run/docker.sock http:/v1.24/containers/json
複製代碼
一、引入 UnixSocket 工具包面試
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.18</version>
</dependency>複製代碼
二、測試代碼docker
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();
}複製代碼
© 著做權歸做者全部,轉載或內容合做請聯繫做者json
● Service Mesh - gRPC 本地聯調遠程服務框架
● 使用 Thymeleaf 動態渲染 HTMLcurl
● Fastjson致命缺陷socket
● Spring Boot 2 集成log4j2日誌框架工具
● Spring Security 實戰乾貨:如何保護用戶密碼
● Spring Boot RabbitMQ - 優先級隊列
本文由博客一文多發平臺 OpenWrite 發佈!