在Java中获取Docker镜像的构建日志,你可以使用Docker Engine API。以下是一个使用OkHttp库的示例代码,用于获取构建日志:
import okhttp3.*;
import java.io.IOException;
public class DockerLogsFetcher {
private static final String DOCKER_API_URL = "http://localhost:2375"; // 或者使用实际的Docker API地址
private static final OkHttpClient client = new OkHttpClient();
public static void main(String[] args) {
String containerId = "container_id"; // 替换为你的容器ID
Request request = new Request.Builder()
.url(DOCKER_API_URL + "/containers/" + containerId + "/logs?stdout=1&stderr=1")
.get()
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
String logs = response.body().string();
System.out.println(logs);
} else {
System.out.println("Failed to fetch logs: " + response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}