-
Notifications
You must be signed in to change notification settings - Fork 1k
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
How can I get input from terminal in a container? #2308
Comments
请问你现在解决这个问题了吗?如何解决的呢? |
I have a solution, but it's not perfect, just use "echo" command. import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = Integer.parseInt(scan.next());
int b = Integer.parseInt(scan.next());
System.out.println((a + b));
}
} and private List<ExecuteMessage> runCode(String containerId, String language, List<String> inputList) {
List<ExecuteMessage> executeMessageArrayList = new ArrayList<>();
SandboxEntry.DockerInfo dockerInfoByName = sandboxEntry.getDockerInfoByName(language);
String runCmd = dockerInfoByName.getRun();
for (String inputStr :
inputList) {
String curRunCmd = String.format("echo \"%s\" | %s", inputStr, runCmd);
ExecuteMessage executeMessage = runCmdWithDocker(containerId, curRunCmd);
executeMessageArrayList.add(executeMessage);
}
return executeMessageArrayList;
}
.....
....
...
private ExecuteMessage runCmdWithDocker(String containerId, String cmd) {
String[] userCodeCmd = {"sh", "-c", String.format("%s", cmd)};
log.info("docker cmd = {}", Arrays.stream(userCodeCmd)
.collect(Collectors.joining(" ")));
final boolean[] timeout = {true};
ExecuteMessage executeMessage = new ExecuteMessage();
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
.withAttachStdout(true)
.withAttachStderr(true)
.withAttachStdin(true)
.withCmd(userCodeCmd)
.exec();
......
.....
} finally, terminal will output:
You can get more detail from my code repository |
Oh, you can get better solution from here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to run an interactive Java program in docker and need input from a terminal. How do I do this? The Java code is here:
The text was updated successfully, but these errors were encountered: