[英]How to run a docker image from java Program?
我正在尝试使用脚本从 Java 程序运行 Ubuntu 映像; 方法如下:
我的Java代码:
public static void main(String[] args) {
executeCommand("/home/abrahem/IdeaProjects/untitled3/src/createContainer.sh");
}
public static void executeCommand(String filePath) {
File file = new File(filePath);
if (!file.isFile()) {
throw new IllegalArgumentException("The file " + filePath + " does not exist");
}
try {
if (isLinux()) {
Process p = Runtime.getRuntime().exec("sh " + filePath);
p.waitFor(); // i tried to remove this but still not work for my me
} else if (isWindows()) {
Runtime.getRuntime().exec("cmd /c start " + filePath);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我的createContainer.sh脚本文件:
#!bin/sh
sudo docker run ubuntu
当我去bin
并输入:
docker ps
或者
docker ps -a
它应该显示正在运行的 Ubuntu 容器,但它没有。
注意:shell位置没有问题; 我尝试在 shell 文件中创建文件并且它可以工作。
您不会从流程中捕获任何错误消息或正常输出。 也许它只是有效?
使用 Process 的getErrorStream()和 getOutputStream() 方法来捕获进程的输出,有点像这里描述的那样。 您可能只会看到预期的输出。 如果不是,则应该是错误流上的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.