2024-12-27 16:06:20 +08:00
|
|
|
package com.guwan.backend.controller;
|
|
|
|
|
2024-12-27 18:24:11 +08:00
|
|
|
import com.github.dockerjava.api.command.CreateContainerCmd;
|
|
|
|
import com.github.dockerjava.api.model.*;
|
2024-12-27 16:06:20 +08:00
|
|
|
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;
|
|
|
|
|
2024-12-27 18:24:11 +08:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2024-12-27 16:06:20 +08:00
|
|
|
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();
|
2024-12-27 18:24:11 +08:00
|
|
|
|
2024-12-27 16:06:20 +08:00
|
|
|
exec.forEach(container ->
|
|
|
|
System.out.println("name = " + Arrays.toString(container.getNames())));
|
|
|
|
return exec;
|
|
|
|
}
|
2024-12-27 18:24:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/create")
|
|
|
|
public void create() throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String javaFilePath = "D:\\00_桌面\\demo.java"; // 这里填入 Java 文件路径
|
|
|
|
File javaFile = new File(javaFilePath);
|
|
|
|
|
|
|
|
Path localDir = Files.createTempDirectory("docker-java");
|
|
|
|
File localJavaFile = new File(localDir.toFile(), "Main.java");
|
|
|
|
Files.copy(javaFile.toPath(), localJavaFile.toPath());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String imageName = "openjdk:8"; // 使用官方 OpenJDK 11 镜像
|
|
|
|
|
|
|
|
|
|
|
|
HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
|
.withBinds(new Bind(localDir.toString(), new com.github.dockerjava.api.model.Volume("/java"))); // 绑定挂载
|
|
|
|
|
|
|
|
|
|
|
|
// 启动容器
|
|
|
|
String containerId = dockerClient.createContainerCmd(imageName)
|
|
|
|
.withCmd("bash", "-c", "javac /java/Main.java && java -cp /java Main && tail -f /dev/null")
|
|
|
|
.withHostConfig(hostConfig)
|
|
|
|
.withName("java-app-container")
|
|
|
|
.exec().getId();
|
|
|
|
|
|
|
|
dockerClient.startContainerCmd(containerId).exec();
|
|
|
|
|
|
|
|
System.out.println("Container started. Check the output in Docker.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-27 16:06:20 +08:00
|
|
|
}
|