Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

How to send user input at runtime to attachStdin() when using execStart(execCreation); #738

Closed
ajmalrehman opened this issue May 8, 2017 · 5 comments

Comments

@ajmalrehman
Copy link

Description

I am running a JavaProgram on docker which requires user input during the execution, for this I have used following two methods which runs perfectly fine for the programs which does not require user Input.
final String execCreation = docker.execCreate( id, command,DockerClient.ExecCreateParam.attachStdin(),DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());

where as command = bash, -c, cd mydockerbuild/ && javac Program.java && java -cp . Program exit

Program.java requires user input during the execution, now i don't how do i pass those inputs at runtime. Please help in this.

[Add feature/bug description here]

How to reproduce

Take any java program which requires user input during the execution and use the command provided above to execute the program. below is my code:

String id=null;
String execOutput = null;
DockerClient docker= null;

public String compileAndExecuteCode(String dockerimage,String[] command,String mountedpath){
	 try {
		docker = DefaultDockerClient.fromEnv().build();
		docker.pull(dockerimage);
		final String[] ports = {"99", "9999"};
		final Map<String, List<PortBinding>> portBindings = new HashMap<>();
	
		for (String port : ports) {
		    List<PortBinding> hostPorts = new ArrayList<>();
		    hostPorts.add(PortBinding.of("0.0.0.0", port));
		    portBindings.put(port, hostPorts);
		}
		
		   
		List<PortBinding> randomPort = new ArrayList<>();
		randomPort.add(PortBinding.randomPort("0.0.0.0"));
		portBindings.put("443", randomPort);
		final HostConfig hostConfig = HostConfig.builder().binds(mountedpath).
				portBindings(portBindings).build();
		

		
		final ContainerConfig containerConfig = ContainerConfig.builder()
			    .hostConfig(hostConfig)
			    .image(dockerimage).exposedPorts(ports)
			    .cmd("sh", "-c", "while :; do sleep 1; done")
			    .build();
		
		final ContainerCreation creation = docker.createContainer(containerConfig);
		 id = creation.id();

		// Start container
		docker.startContainer(id);
		// Exec command inside running container with attached STDOUT and STDERR
		System.out.println(docker.inspectContainer(id));
		
		//final String[] command = {"bash", "-c", "cd mydockerbuild/ && javac "+filename+"  && java -cp . "+executablefilename+" exit"};
		System.out.println(command);
		final String execCreation = docker.execCreate(
		    id, command,DockerClient.ExecCreateParam.attachStdin(),DockerClient.ExecCreateParam.attachStdout(),
		    DockerClient.ExecCreateParam.attachStderr());
		final LogStream output = docker.execStart(execCreation);
		final ExecState execinfo =docker.execInspect(execCreation);
		System.out.println(execinfo);
		if(execinfo.running()&& execinfo.exitCode()==null){
			System.out.println("Waiting for user input....................");
			
			
		}
				
		try{
			execOutput = output.readFully();
			
			}catch(RuntimeException e){
				e.printStackTrace();
				
				
			}
			System.out.println(execOutput);
	} catch (DockerCertificateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (DockerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	finally{
		
		try {
			// Kill container
			docker.killContainer(id);
			// Remove container
			docker.removeContainer(id);
			// Close the docker client
			docker.close();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DockerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

}

	return execOutput;
	
}

What do you expect

There should some method in docker client to pass the user Input to the commands that requires user input.

What happened instead

Code exit with execution with exitcode==null and status ==running

Software:

  • docker version: 17.03.1-ce
  • Spotify's docker-client version: 3.5.1

Full backtrace

[Paste full backtrace here]
@ajmalrehman
Copy link
Author

is anybody from docker-client team going to reply? its quiet depressing after all the effort put in building my application now i am stuck because of this.

@johnflavin
Copy link
Contributor

This isn't officially supported. But there are workarounds. See #156 for discussion.

@mattnworb
Copy link
Member

@ajmalrehman we would welcome any pull requests to add functionality for this or any other operation that is missing. We do not aim to support every single use case or Docker API operation out of the box.

I am glad to hear that you find this library useful enough to use in your application but if you expect every open-source library you use to always support every use case of yours for free then you will find yourself feeling depressed often.

@mattnworb
Copy link
Member

I am going to close this issue as it is a duplicate of #156 (thanks @johnflavin for finding that issue).

@ajmalrehman
Copy link
Author

sorry for my words hope u understand how we programmers end up when we are stuck!. Thanks for routing it to the appropriate issue.It was of great help.

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

No branches or pull requests

3 participants