Check the API call method on Docker’s official website

For example, to query the list of running containers, use the following HTTP method:

$curl, Unix socket/var/run/docker. The sock HTTP: / v1.24 / containers/json [{ "Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772", "Names":["/tender_wing"], "Image":"bfirsh/reticulate-splines", ... }]Copy the code

Analyze the process of API requests

Run the following command on the local computer

Curl -v -- - socket/var/run/Unix docker. The sock HTTP: / v1.24 / containers/jsonCopy the code

Java simulation call API code implementation

1. Introduce the UnixSocket tool package

< the dependency > < groupId > com. Making. Went < / groupId > < artifactId > went - unixsocket < / artifactId > < version > 0.18 < / version > </dependency>Copy the code

2. Test code

Public static void main(String[] args) {// Establish Socket connection File sockFile = new File("/var/run/docker.sock"); UnixSocketAddress address = new UnixSocketAddress(sockFile); UnixSocketChannel channel = UnixSocketChannel.open(address); UnixSocket unixSocket = new UnixSocket(channel); / / call the Docker API PrintWriter w = new PrintWriter (unixSocket. GetOutputStream ()); W.p rintln (" GET/v1.24 / containers/HTTP / 1.1 json "); w.println("Host: http"); w.println("Accept: */*"); w.println(""); w.flush(); / / close the Output, otherwise it will lead to the read operation has been blocking unixSocket. Below 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(); }Copy the code

The related documents

  • Docker API documentation

© copyright belongs to the author, reprint or content cooperation please contact the author

● Service mesh-GRPC Local integrated remote Service

● Use Thymeleaf to render HTML dynamically

● Fastjson fatal flaw

● Spring Boot 2 integrates log4J2 logging framework

● Java interview key points summary set of core reference answers

● Java interview key points summary of the framework of reference answers

● How to protect your password

● Spring Boot RabbitMQ – Priority queue

This article is published by OpenWrite!