0

I am simulating an online IDE using docker.

Each time user submits their code, an image will be generated and be run. It is working as expected, however when the code asks for input e.g

print("Begin script")
x = input("Enter your name")
print("you entered")
print(x)

python code to run docker:

container = subprocess.Popen(["docker", "run","-it", "--rm", "--name",imageName,imageName])

enter image description here

enter image description here I am unable to pass in input to the python script. It does not even print the prompt "Test input" into terminal I have tried using docker attach command, and I am enter able to enter into terminal, but it is not sending input to python script

3
  • How are you running the container? By default, of course docker run -it ... will accept input just fine. Commented Sep 17, 2021 at 10:20
  • subprocess.run(["docker", "run","-ti", "--rm", "--name",imageName,imageName], capture_output=True) Commented Sep 17, 2021 at 10:21
  • it does not even print the prompt of the input message Commented Sep 17, 2021 at 10:22

2 Answers 2

1

Via comments:

subprocess.run(["docker", "run","-ti", "--rm", "--name",imageName,imageName], capture_output=True)

You're using a function that is described to (emphasis mine)

Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

You also say

it does not even print the prompt of the input message

This is because you've explicitly told subprocess.run() to capture the output instead of allowing it to be printed to stdout. If you were to print the return value of that call, you'd possibly see the output.

If you just want some static input to be piped to the process and get the output, you could use

proc = subprocess.run(..., input="123\n", capture_output=True)
print(proc.stdout)

to simulate someone entering 123 and a newline.

If you want an interactive experience, you will need to use subprocess.Popen() instead. Wiring the stdin/stdout/stderr pipes is somewhat tricky, especially if you need a TTY-like experience.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for this. I updated the post with images after using Popen, it's still not passing in input to the script. It looks like I can't simply do it like this? Would it be better if I just set up docker to accept input via http?
If you want an interactive session, you'll need more than just regular HTTP request/responses, e.g. websockets.
Understood, however as of now I'm just trying to figure out why it wouldn't even get my input at all
the idea is to have a socket to write/listen, and the python server will process these write/listen to the docker directly
Because you're not necessarily connecting the docker run process's stdin to anything.
|
0

I am really sorry that it doesn't answer your question directly, but I am sure it would help.
I think you are trying to make an entire docker container for every user. You don't strictly need that if you only plan to allow them to use your service as a simple IDLE.
You could use something like socat or xinetd to achieve the same. Here is an example that uses socat to run a python script for anyone who connects to the server: https://gitlab.com/Milkdrop/xmas-2020/-/tree/master/misc/complaint

Also, I recommend AGAINST using exec() or eval() to allow others to execute code on your system. Actually, you should not use them in general either.

3 Comments

it is not limited to just python, it should work on other languages as well e.g c++. this is a proof of concept I am trying to come up with, as well as to learn docker
I think I misread the "IDE" in your question as "IDLE". So, the users get to provide their entire code files and your service executes it? IDLE for compiled languages sounds nightmarish.
yes, a simple ide with code exec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.