我不需要调用Docker远程API,而是需要开发一个与Docker Linux Client(而不是Docker守护进程)对话的程序.这是我的代码

try {

String[] command = {"docker", "run", "-it", "tomcat:9", "bash"};

ProcessBuilder pb = new ProcessBuilder(command);

pb.inheritIO();

Process proc = pb.start();

InputStream is = proc.getInputStream();

OutputStream os = proc.getOutputStream();

BufferedReader reader

= new BufferedReader(new InputStreamReader(is));

BufferedWriter writer

= new BufferedWriter(new OutputStreamWriter(os));

writer.write("pwd");

writer.flush();

String line = "";

while ((line = reader.readLine()) != null) {

System.out.print(line + "

");

}

proc.waitFor();

} catch (Exception e) {

e.printStackTrace();

}

我总是得到错误.

如果我使用“-it”,它会说“无法在非tty输入上启用tty模式”,如果我使用“-i”,我将获得Stream Closed Exception.

有什么方法可以解决这个问题吗?

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐