2

I'm new to dockers world. I tried to put my script inside docker, my script takes input argument if running directly in a command prompt -- there are two variables inside my scripts that using input("enter the input here") argument.

When I tried my scripts directly via cmd python scripts.py it runs well, but I tried to dockerize my script using docker-compose but it raised

bigquery_1  | Enter input: Traceback (most recent call last):
bigquery_1  |   File "./scripts/bq.py", line 6, in <module>
bigquery_1  |     default_filterDate_1 = (str(input(" Enter input: ")) or `some default arguments`)
bigquery_1  | EOFError: EOF when reading a line
bigquery_1 exited with code 1

This is what my Dockerfile looks like:

FROM python:3.7

WORKDIR /usr/src/app

COPY ./code /usr/src/app/

RUN pip3 install -r ./requirements.txt

This is the docker-compose.yml

version: '3.8'

services: 
    bigquery:
        build: ./
        command: python ./scripts/bq.py

Is there any workaround on how to pass input variables using docker?

ebuzz168
  • 1,134
  • 2
  • 17
  • 39
  • Your best bet is to use `configparser` and bind a volume to your image, containing your cfg – crissal Jul 19 '21 at 07:07
  • I'm sorry, I don't follow @crissal – ebuzz168 Jul 19 '21 at 07:23
  • Another straightforward approach would be to set `environment:` variables in the `docker-compose.yml` file, which you can read back via `os.environ` in the Python script. The linked question describes the various ways to run a container via Compose and which ones will actually accept interactive input, if you really can't avoid it. – David Maze Jul 19 '21 at 11:01

1 Answers1

2

From a similar question: https://stackoverflow.com/a/39150040/13731995. Basically you need to add tty and stdin_open to your docker compose file.

version: '3.8'

services: 
    bigquery:
        build: ./
        tty: true
        stdin_open: true
        command: python ./scripts/bq.py
gzcz
  • 496
  • 3
  • 7
  • I rebuild my docker-compose as you suggested, now I'm stuck at ` `Starting bigquery_1 ... done` `Attaching to bigquery_1` ` – ebuzz168 Jul 19 '21 at 07:22
  • 1
    How are you running the container? Something like `docker-compose run bigquery`? If you are using `docker-compose up` maybe try running in detached mode with the `-d` flag? – gzcz Jul 19 '21 at 07:26
  • Yes, I was using `docker-compose up`. I tried using `docker-compose run bigquery` and now it works like a charm! Thank you so much, man! – ebuzz168 Jul 19 '21 at 07:34