I have a rudimentary python script named my_python_script.py
that looks like this:
#!/usr/bin/env python
def main():
x = input('What is your name? ')
print('x = {}'.format(x))
if __name__ == "__main__":
main()
When I run it locally from my command line it performs exactly as you would expect.
However, now I'm trying to run this same script from within a Docker container. So I created a Dockerfile
that looks like this:
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
My docker-compose.yml
file looks like this:
version: "3.7"
services:
myservice:
build:
context: .
entrypoint: /bin/bash
command: -c "./my_python_script.py"
volumes:
- .:/usr/src/app
When I run it with docker-compose up
, it fails like this:
Creating myservice_1 ... done
Attaching to myservice_1
myservice_1 | What is your name? Traceback (most recent call last):
myservice_1 | File "/usr/src/app/./my_python_script.py", line 7, in <module>
myservice_1 | main()
myservice_1 | File "/usr/src/app/./my_python_script.py", line 3, in main
myservice_1 | x = input('What is your name? ')
myservice_1 | EOFError: EOF when reading a line
myservice_1 exited with code 1
How can I make this python script accept interactive input from my keyboard when it is running inside a Docker container?