1

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • I imagine you would need the `-it` flag when you start the container – Paolo Jun 21 '21 at 08:30
  • -it for docker compose discussed [here](https://stackoverflow.com/a/39150040/3214422) – SCKU Jun 21 '21 at 08:37
  • I added the following two lines to my `docker-compose.yml` which correspond to the `-it` flags: `stdin_open: true` and `tty: true`. When I did that, it simply sits there and waits instead of prompting the user for their name. – Saqib Ali Jun 21 '21 at 08:39
  • This is specifically about python - please reopen – Wolfgang Fahl Jun 21 '21 at 10:07
  • @tripleee You used the wrong duplicate. – Paolo Jun 21 '21 at 10:12
  • This is specifically about Docker, and the solution is the `-t` option, isn't it? – tripleee Jun 21 '21 at 10:18
  • @tripleee yeah, but the container is started with `docker-compose`. This would be a more appropriate dupe https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose/39150040#39150040 – Paolo Jun 21 '21 at 11:55

1 Answers1

-1

According to this you might want to try:

docker-compose run myservice

I am no docker expert, but I think that docker-compose up will simply start all services from docker-compose.yaml and then output the log of all services.

See also this part of the docker-compose FAQ.

  • The question is how to make one of those services read input from standard input. – tripleee Jun 21 '21 at 12:01
  • 1
    Thank you. But isn't the question is how to run the shown script from within a docker container? I believe, if so, this is an appropriate approach. Also, simply adding the `stdin_open: true` and `tty: true` parameters didn't work for me and seems to not work for the OP as well. See also the upvoted comment on the answer of the duplicate you marked. – Dominik Berse Jun 21 '21 at 12:16
  • It doesn't read from standard input for me. – tripleee Jun 21 '21 at 12:22
  • This is what I am referring to: https://stackoverflow.com/a/36265910/12661819 – Dominik Berse Jun 21 '21 at 12:26
  • Indeed, like it says, that doesn't give you a proper interactive session, unless you resort to separately running one with `docker exec -it`. – tripleee Jun 21 '21 at 12:28
  • Sorry, I am lost here. It says you can *also* do that, as another option. If I run `docker-compose run myservice` like above on my Ubuntu 20.04, I get the output `What is your name?` and then `x = Dominik` after I entered my name. Whereas `docker-compose up` even with said parameters gives me a blank and no input option. But maybe I am just misunderstanding something. – Dominik Berse Jun 21 '21 at 12:37
  • Also the linked docker-compose FAQ states "The run command acts like docker run -ti in that it opens an interactive terminal to the container [...]." – Dominik Berse Jun 21 '21 at 12:49
  • When I tried this, I got `docker: Error response from daemon: pull access denied for myservice, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.`. If I don't use docker compose, it won't use my `docker-compose.yml` file, so it will have no clue what `myservice` is. – Saqib Ali Jun 21 '21 at 14:26
  • Did you accidentially use `docker run myservice`? If so, please try `docker-compose run myservice` like above. – Dominik Berse Jun 21 '21 at 20:28