0

How to execute docker exec -it netvertex bash by Java Program. By this i am trying to execute but not working

public void execute(String command) {
        try {

            System.out.println("===========>" + command);
            Channel channel1 = session.openChannel("exec");

            ((ChannelExec) channel1).setPty(true);
            ((ChannelExec) channel1).setCommand(command);
            // X Forwarding
            // channel.setXForwarding(true);

            //channel.setInputStream(System.in);
            channel1.setInputStream(null);

            //channel.setOutputStream(System.out);

            //FileOutputStream fos=new FileOutputStream("/tmp/stderr");
            //((ChannelExec)channel).setErrStream(fos);
            ((ChannelExec) channel1).setErrStream(System.err);

            InputStream in1 = channel1.getInputStream();

            channel1.connect();

            byte[] tmp1 = new byte[1024];
            while (true) {
                while (in1.available() > 0) {
                    int i = in1.read(tmp1, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp1, 0, i));
                }
                if (channel1.isClosed()) {
                    if (in1.available() > 0) continue;
                    System.out.println("exit-status: " + channel1.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }

        } catch (Exception e) {

        }
    }

By above code my other Linux command are working. But docker exec it not working. And the other option i tried

try {

            String host = "root@10.121.21.224";
            String passwd = "root#10";
            Terminal exec = new Terminal(pageUiUtils);
            exec.loginSSH(host, passwd);

            String[] command = {"docker", "exec", "-it", "netvertex", "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 + "\n");
            }

            exec.execute("docker exec -it netvertex bash");
            pageUiUtils.pause(1000);
            exec.execute("pwd");
            //proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }

by above code also showing error that The input device is not a TTY

4
  • the error because you add -it to the command
    – LinPy
    Sep 17, 2019 at 12:11
  • Generally, you don't, and you can't without trusting the program with unrestricted root-level access over the host. It's better to write a proper API in the service you're calling into and making something like an HTTP call to it.
    – David Maze
    Sep 17, 2019 at 12:15
  • It might also be a good idea to use the Docker API in java: github.com/docker-java/docker-java/wiki.
    – Blokje5
    Sep 17, 2019 at 14:06
  • You want to open a bash inside the container to execute pwd or what is going to be the full scale vision?!
    – dpr
    Sep 17, 2019 at 15:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.