2

I'm trying to run a command in a docker container using Java's ProcessBuilder. However, it hangs when waiting for exit code. Why is that?

ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList("/usr/bin/docker", "run", "base", "echo", "hello"));

Process process = processBuilder.start();

// ... Spin off another thread to collect stdout and stderr

int exitCode = process.waitFor(); // <-- HANG

I can see that the process spits out the correct results to stdout. It just never returns an exit code.

4
  • One possible problem: you don't appear to be gobbling the process's InputStream and the ErrorStreams. You could be overrunning the buffers available for these streams. For more on this, please read When Runtime.exec() won't. Read it all as it's all important. Commented Jul 13, 2013 at 1:16
  • @Hovercraft, I start up another thread to gobble those. I tested my code running the echo hello command without docker, and it works. Commented Jul 13, 2013 at 1:20
  • What happens when you run /usr/bin/docker run base echo hello from the command line? Does it exit? Commented Jul 13, 2013 at 1:59
  • @StephenC, yes, it exits with a 0 exit code. Commented Jul 13, 2013 at 2:36

1 Answer 1

2

Why use the CLI which is just a wrapper around the API? Better to use it directly from java.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.