0

I am trying to execute python script in docker, and allow inputs from the terminal to the dockr

Dockerfile:

FROM python:3
WORKDIR /app
USER root
ADD . .
RUN chmod a+x ./main.py
RUN chmod a+x ./run.sh
ENTRYPOINT ["sh","./run.sh"]

main.py:

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

run.sh:

#! usr/bin/env bash
timeout --signal=SIGTERM 500 python3 main.py
exit $?

Docker build and run commands:

docker image build . -t testimg --rm
docker run -ti  --name testimg testimg

terminal logs:

Begin script
Enter your namejohn

It gets stuck, it does not register what I typed into my terminal "john"

xineta5158
  • 117
  • 1
  • 6
  • Did you tried with `docker run -ti --name testimg testimg /bin/bash`? [Check this](https://stackoverflow.com/a/36265910/10217732) might help you. – Suyog Shimpi Sep 18 '21 at 02:35
  • Can you just do this in `run.sh`: `#!/usr/bin/bash python main.py` (each on it's own line) – djs Sep 18 '21 at 02:37

1 Answers1

0

The problem is in your run.sh script. I changed the script as follows:

#! usr/bin/env bash
timeout --signal=SIGTERM --foreground 500 python3 main.py
exit $?

And now it works. I added the --foreground option to the timeout command. You can find more on timeout and its options in the following links:

https://linuxize.com/post/timeout-command-in-linux/

https://man7.org/linux/man-pages/man1/timeout.1.html

Rony
  • 163
  • 1
  • 5