Docker-Python script input error

主宰稳场 提交于 2019-12-08 12:20:40

问题


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?


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/48731886/docker-python-script-input-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!