标题翻译
Run Docker run command from Java program
问题
我必须在我的Java程序中运行以下命令:
docker run --rm -it -p 5000:5000 test
这个命令应该持续运行。
我创建了一个run.sh
文件并将命令放在其中:
StringBuffer output = new StringBuffer();
ProcessBuilder processBuilder = new ProcessBuilder("nohup", "sh", "run.sh");
try {
Process start = processBuilder.start();
InputStream input = start.getInputStream();
int chars;
while ((chars = input.read()) != -1) {
output.append((char) chars);
}
input.close();
logger.info(output.toString());
} catch (IOException e) {
e.printStackTrace();
}
运气不佳
我还尝试了:
StringBuffer output = new StringBuffer();
Process process = Runtime.getRuntime().exec("docker run --rm -it -p 5000:5000 test");
InputStream input = process.getInputStream();
int chars;
while ((chars = input.read()) != -1) {
output.append((char) chars);
}
input.close();
logger.info(output.toString());
运气仍然不佳。
日志记录器没有打印任何内容。对于任何建议或想法,我都表示感谢。提前致谢!
英文翻译
I have to run docker run --rm -it -p 5000:5000 test
the command in my Java program.
This command should run continuously,
I created run.sh
file and put cmd in it,
StringBuffer output = new StringBuffer();
ProcessBuilder processBuilder = new ProcessBuilder("nohup", "sh", "run.sh");
try {
Process start = processBuilder.start();
InputStream input = start.getInputStream();
int chars;
while ((chars = input.read()) != -1) {
output.append((char) chars);
}
input.close();
logger.info(output.toString());
} catch (IOException e) {
e.printStackTrace();
}
no luck
Also I tried:
StringBuffer output = new StringBuffer();
Process process = Runtime.getRuntime().exec("docker run --rm -it -p 5000:5000 test");
InputStream input = process.getInputStream();
int chars;
while ((chars = input.read()) != -1) {
output.append((char) chars);
}
input.close();
logger.info(output.toString());
no luck also.
Logger is not printing anything. Any suggestion or idea are appreciated. Thanks in advance!
答案1
得分: 0
因为 getRuntime
不会加载 env
变量,尝试使用完整的 docker
命令路径。
例如:
Process process = Runtime.getRuntime().exec("/usr/bin/docker run --rm -it -p 5000:5000 test");
如果你的 docker
实例不在 /usr/bin
目录下,你可以使用以下命令找到它:
whereis docker
which docker
type docker
英文翻译
Since getRuntime
doesn't load env
vars, try to use full path of docker
command.
I.e.:
rocess process = Runtime.getRuntime().exec("/usr/bin/docker run --rm -it -p 5000:5000 test");
If your docker
instance is not in /usr/bin
, you can find it using such commands:
whereis docker
which docker
type docker
答案2
得分: 0
首先,您需要更改代码以获取错误信息。
如果您将
InputStream input = process.getInputStream();
更改为
BufferedReader input = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
您可以获取错误信息,您还可以单独获取错误信息。如果您查看错误信息,您会看到它说:
输入设备不是TTY
我认为这意味着 Runtime.getRuntime()
不是TTY,因此您无法在交互式(-it
)模式下运行Docker。
英文翻译
First, you need to change the code to get the error too.
if you change
InputStream input = process.getInputStream();
to
BufferedReader input = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
you can get the error, you can also get the error separately. If you look at the error, you can see it says:
the input device is not a TTY
which I think it mens, the Runtime.getRuntime()
is not a TTY so you cant run the docker in the interactive (-it
) mode.
专注分享java语言的经验与见解,让所有开发者获益!
评论