Kon Kyri
Kon Kyri

Reputation: 159

Docker-Python script input error

I am new to Docker.I have a script named ApiClient.py. The ApiClient.py script asks the user to input some data such as user's email,password,the input file(where the script will get some input information) and the output file(where the results will be output).I have used this Dockerfile:

FROM python:3
WORKDIR /Users/username/Desktop/Dockerfiles
ADD . /Users/username/Desktop/Dockerfiles
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
ENV NAME var_name
CMD ["python", "ApiClient.py"]

1st Issue: I have used this WORKDIR and ADD because thats where the input and output files exist.Is it wrong to declare these directories?

2n Issue: The script asks for the user to input some info such as the email and password.However when i run:

docker run -p 4000:80 newapp

I get the following error: username = ("Please enter your username")

EOFError:EOF when reading a line

Why am i geting this error?

Upvotes: 5

Views: 8573

Answers (2)

Shahriar
Shahriar

Reputation: 13814

Lets make some necessary files as example

Dockerfile

FROM python
ADD . /python
WORKDIR /python

ENTRYPOINT ["python", "main.py"]

You want to run a script that will take two argument for input & output file

main.py

import sys
input_file = sys.argv[1]
output_file = sys.argv[2]
file = open(input_file, 'r') 
print(file.read())

file = open(output_file, 'w') 

file.write('Hello World')

Now build and run

$ docker build  -t test  .
$ docker run -it -v /tmp/python/:/python/data test data.txt data/output.txt

Your main.py will take input from /python/data.txt and write output in /python/data/output.txt

As /python/data is mounted into /tmp/python/, you will get output.txt in /tmp/python/ in your local

Upvotes: 2

Abdullah Shah
Abdullah Shah

Reputation: 780

Use docker run -i -t <your-options>

So here, -i stands for interactive mode and -t will allocate a pseudo terminal for us.

Which in your scenario would be

docker run -p 4000:80 -it newapp

Hope it helps!

Upvotes: 8

Related Questions