package org.squonk.execution.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
public class DockerStdin {
public static void main(String[] args) throws Exception {
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .build();
DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withCmd("sleep", "99")
.exec();
System.out.println("Created container " + container.getId());
InputStream stdin = new ByteArrayInputStream("STDIN\n".getBytes("UTF-8"));
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
dockerClient.startContainerCmd(container.getId()).exec();
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
.withAttachStdout(true)
.withAttachStdin(true)
.withCmd("cat")
.exec();
boolean completed = dockerClient.execStartCmd(execCreateCmdResponse.getId())
.withDetach(false)
.withTty(true)
.withStdIn(stdin)
.exec(new ExecStartResultCallback(stdout, System.err))
.awaitCompletion(5, TimeUnit.SECONDS);
System.out.println("Completed = " + completed);
System.out.println("output = " + stdout.toString("UTF-8"));
}
}echo 'Hello World!' | docker run --rm -i busybox cat > result.txt
the NettyDockerCmdExecFactory. Hope that helps.
Regards
Marcus
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://docker.somewhere.tld:2376") .withDockerTlsVerify(true) .withDockerCertPath("/home/user/.docker") .build(); // using jaxrs/jersey implementation here (netty impl is also available) DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory() .withReadTimeout(1000) .withConnectTimeout(1000) .withMaxTotalConnections(100) .withMaxPerRouteConnections(10); DockerClient dockerClient = DockerClientBuilder.getInstance(config) .withDockerCmdExecFactory(dockerCmdExecFactory) .build();
InputStream stdin = new ByteArrayInputStream("ls\n".getBytes("UTF-8")); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()) .withAttachStdout(true) .withAttachStdin(true) .withCmd("/bin/bash") .exec(); boolean completed = dockerClient.execStartCmd(execCreateCmdResponse.getId()) .withDetach(false) .withTty(true) .withStdIn(stdin) .exec(new ExecStartResultCallback(stdout, System.err)) .awaitCompletion(5, TimeUnit.SECONDS);
But I couldn't get nothing although I have sent the stdin data to the running container.Could you tell me why?Besides,when I follow the test posted in https://github.com/docker-java/docker-java/blob/master/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java
I don't know the idea of the config in method.such as withTty and so on.Could you send me a doc about how to use the method or how to config the method.Thanks.// using jaxrs/jersey implementation here (netty impl is also available)
DockerCmdExecFactory dockerCmdExecFactory = new NettyDockerCmdExecFactory()