61 lines
1.7 KiB
Java
61 lines
1.7 KiB
Java
|
package com.guwan.backend.controller;
|
||
|
|
||
|
import com.github.dockerjava.api.model.Container;
|
||
|
import com.github.dockerjava.api.model.Image;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||
|
import org.springframework.web.bind.annotation.RestController;
|
||
|
import com.github.dockerjava.api.DockerClient;
|
||
|
import com.github.dockerjava.api.model.Info;
|
||
|
|
||
|
import java.util.Arrays;
|
||
|
import java.util.List;
|
||
|
|
||
|
@RestController
|
||
|
@RequestMapping("/api/common")
|
||
|
public class DockerController {
|
||
|
|
||
|
private final DockerClient dockerClient;
|
||
|
|
||
|
@Autowired
|
||
|
public DockerController(DockerClient dockerClient) {
|
||
|
this.dockerClient = dockerClient;
|
||
|
}
|
||
|
|
||
|
@GetMapping("/docker-info")
|
||
|
public String getDockerInfo() {
|
||
|
Info info = dockerClient.infoCmd().exec(); // 获取 Docker 守护进程的信息
|
||
|
|
||
|
|
||
|
System.out.println("info = " + info);
|
||
|
|
||
|
return info.toString(); // 将 Docker 信息返回到 HTTP 响应
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取镜像列表
|
||
|
*
|
||
|
* @param
|
||
|
* @return
|
||
|
*/
|
||
|
@GetMapping("/imageList")
|
||
|
public List<Image> imageList() {
|
||
|
List<Image> imageList = dockerClient.listImagesCmd().withShowAll(true).exec();
|
||
|
|
||
|
System.out.println("imageList = " + imageList);
|
||
|
return imageList;
|
||
|
}
|
||
|
|
||
|
|
||
|
@GetMapping("/listContainers")
|
||
|
public List<Container> listContainers() {
|
||
|
List<Container> exec = dockerClient.listContainersCmd().exec();
|
||
|
|
||
|
exec.forEach(container ->
|
||
|
System.out.println("name = " + Arrays.toString(container.getNames())));
|
||
|
return exec;
|
||
|
}
|
||
|
}
|