Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a certain output as the input of docker exec... #2306

Open
Aurora-up opened this issue Feb 3, 2024 · 3 comments
Open

use a certain output as the input of docker exec... #2306

Aurora-up opened this issue Feb 3, 2024 · 3 comments

Comments

@Aurora-up
Copy link

I want to use docker-java to execute like docker exec -i <containerId> java -cp /codeStore Main < ~/input.txt
When I write String[] cmd Args = new String[] { "java", "-cp", "/codeStore", "Main", "<", "~/input.txt"}; like this As a result, the program kept running and could not be stopped, so I used dockerClient.execStartCmd(execCreateCmdResponse.getId()).withStdIn(inputStream)
The writing method of withStdIn(inputStream) is used in , but the following error occurs;
My requirement is to use a certain output as the input of docker exec.... I sincerely ask the author which API can do this.

....
dockerClient.startContainerCmd(containerId).exec();


System.out.println("----------------------------------------------");
String[] cmdArgs = new String[] { "java", "-cp", "/codeStore", "Main"};
var execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
		.withCmd(cmdArgs)
		.withAttachStdin(true)
		.withAttachStdout(true)
		.withAttachStderr(true)
		.exec();
System.out.println("----------------------------------------------");
System.out.println( execCreateCmdResponse.getRawValues());


var execStartResultCallback = new ExecStartResultCallback() {
	@Override
	public void onNext(Frame frame) {
		if (frame.getStreamType() == StreamType.STDERR) {
			System.out.println( new String(frame.getPayload()));
		} else {
			System.out.println(new String(frame.getPayload()));
		}
		super.onNext(frame);
	}
};

String inputString = "2 5\n1 2\n2 4";

byte[] inputBytes = inputString.getBytes(StandardCharsets.UTF_8);

InputStream inputStream = new ByteArrayInputStream(inputBytes);

try {
	dockerClient.execStartCmd(execCreateCmdResponse.getId())
			.withStdIn(inputStream)
			.exec(execStartResultCallback)
			.awaitCompletion();
} catch (InterruptedException e) {
	e.printStackTrace();
}
16:03:08.420 [docker-java-stream-2145430985] ERROR com.github.dockerjava.api.async.ResultCallbackTemplate - Error during callback
java.lang.UnsupportedOperationException: Does not support hijacking
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.execute(JerseyDockerHttpClient.java:288)
        at com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:228)
        at com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
        at java.base/java.lang.Thread.run(Thread.java:840)
Exception in thread "main" java.lang.UnsupportedOperationException: Does not support hijacking
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.execute(JerseyDockerHttpClient.java:288)
        at com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:228)
        at com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
        at java.base/java.lang.Thread.run(Thread.java:840)
@YaleXin
Copy link

YaleXin commented May 15, 2024

I get the similar error when I want to use inputStream

@Aurora-up
Copy link
Author

Since the default HTTP framework is not supported inputstream, you can use httpclient5 as the HTTP framework for Docker-Java.
Specific solutions:

  <dependency>
      <groupId>com.github.docker-java</groupId>
      <artifactId>docker-java</artifactId>
      <version>3.3.4</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java-transport-httpclient5 -->
  <dependency>
      <groupId>com.github.docker-java</groupId>
      <artifactId>docker-java-transport-httpclient5</artifactId>
      <version>3.3.4</version>
  </dependency>
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build();
DockerHttpClient dockerHttpClient = new ApacheDockerHttpClient.Builder() 
	.dockerHost(config.getDockerHost())
	.sslConfig(config.getSSLConfig())
	.maxConnections(3000) 
	.build();
dockerClient = DockerClientBuilder.getInstance(config).withDockerHttpClient(dockerHttpClient).build();

@YaleXin
Copy link

YaleXin commented May 16, 2024

ok, thank you, I will try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants