Closed
Description
Hello,
I have the following manual docker command to convert an image to a pdf:
cat t1.png | docker run -i jitesoft/tesseract-ocr stdin stdout pdf > output.pdf
Now I want to execute this command using docker java version 3.3.1 using AppacheDockerHttpClient. If I pass the source file using a volume, the redirection of stdout perfectly works. Now I also want to redirect stdin and directly stream the input file. Unfortunately nothing ever happens. The result callback is never called, the program just runs in to timeout.
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("unix:///var/run/docker.sock").build();
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder().dockerHost(config.getDockerHost())
.sslConfig(config.getSSLConfig()).maxConnections(100).connectionTimeout(Duration.ofSeconds(500))
.responseTimeout(Duration.ofSeconds(500)).build();
DockerClient dockerClient = DockerClientBuilder.getInstance(config).withDockerHttpClient(httpClient).build();
CreateContainerCmd cmd = dockerClient.createContainerCmd("jitesoft/tesseract-ocr:latest") //
.withTty(false) //
.withAttachStdin(true) //
.withAttachStdout(true) //
.withAttachStderr(true) //
.withStdinOpen(true) //
.withCmd("stdin", "stdout", "pdf"); //
cmd.withHostConfig(HostConfig.newHostConfig().withAutoRemove(true));
CreateContainerResponse resp = cmd.exec();
try (ByteArrayOutputStream os = new ByteArrayOutputStream();
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis)) {
Adapter<Frame> exec = dockerClient.attachContainerCmd(resp.getId()) //
// .withLogs(true) //
.withStdErr(true) //
.withStdOut(true) //
.withFollowStream(true) //
.withStdIn(pis) //
.exec(new ResultCallback.Adapter<>() {
@Override
public void onNext(Frame object) {
try {
System.out.println(object.toString());
os.write(object.getPayload());
super.onNext(object);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
exec.awaitStarted();
dockerClient.startContainerCmd(resp.getId()).exec();
pos.write(content); // content is a byte array containing the image data loaded from the file t1.png
pos.flush();
exec.awaitCompletion(10, TimeUnit.SECONDS);
exec.close();
System.out.println(os.toString(StandardCharsets.UTF_8));
} finally {
dockerClient.stopContainerCmd(resp.getId());
dockerClient.removeContainerCmd(resp.getId());
}