0

When I run my container I'm trying to run "docker.py" from the root directory ("/") with "/usr/bin/python". I've checked the source image to ensure the python binary exists, I've uploaded docker.py to the docker image and run it with that binary successfully. However, when I try to create the image and then run the image, I get this error ("EOFError: EOF when reading a line") and I don't know what this means or where to start. Any help is appreciated. Thanks in advance!

cat docker.py

#!/usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()

cat Dockerfile

FROM python:2.7
ADD docker.py /docker.py
CMD ["/usr/bin/python", "/docker.py"]

docker build .

Sending build context to Docker daemon  175.8MB
Step 1/3 : FROM python:2.7
 ---> b1d5c2d7dda8
Step 2/3 : ADD docker.py /docker.py
 ---> f55a19158773
Step 3/3 : CMD ["/usr/bin/python", "/docker.py"]
 ---> Running in b830da5a2f5b
 ---> ef5b878d203f
Removing intermediate container b830da5a2f5b
Successfully built ef5b878d203f
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

docker run ef5b878d203f

Enter your name: Traceback (most recent call last):
  File "/docker.py", line 2, in <module>
    my_name = raw_input("Enter your name: ")
EOFError: EOF when reading a line
Dan
  • 331
  • 6
  • 17
  • 1
    If you want to use the terminal in the container use `docker run` with the `-ti` arguments. – Klaus D. Nov 12 '17 at 05:59
  • Thank you, that worked. Now, is there a way I can attach the terminal in the container to a network socket so that remote machines can attach to the python script I'm running in my container and "interactively" work with it over the network? – Dan Nov 12 '17 at 06:16
  • One problem, one question. – Klaus D. Nov 12 '17 at 06:21

1 Answers1

1

The trouble is that your docker.py script is expecting input. As @Klaus D. was commenting, for the raw_input command you need to be in an interactive shell. When you're running inside a docker container, there is no interactive shell, so when you run raw_input it just gets an unexpected end to that command hence the error you receive.

Once you start the container, from a user perspective it is like a little virtual machine, the only interaction you can have with it is through methods that you have set up within the container itself. Or else to enter the container using:

docker exec -it CONTAINER_HASH /bin/bash

from there you've got an interactive shell and if you log in like that THEN you can run

python /docker.py

and it works as you would expect.

Jeff Richards
  • 2,032
  • 2
  • 18
  • 23
  • Thanks KlausD and Jeff, I understand. I have tried to expose my container via a network port. Even though 'docker ps -a' indicates the container is running and the requested port is exposed, I am unable to "telnet IP port" to connect to it. If I use "nc IP port" I get a connection but I don't get prompted by my script proactively, I have to enter some data to get anything back, that is followed by a broken pipe rather than the rest of the actions called for in my script. I've also tried an xinetd service with very similar behavior. I know 1 question 1 answer, just looking for direction. – Dan Nov 12 '17 at 16:24
  • Hi Dan, the issue here is likely that your container needs a service listening to the port you've exposed, so to follow the path you're on you'd need the container to be running a service that would hear your request and then hand the request to the script and return a response. So the port bring exposed is not enough to do what you're hoping. This question did a nice job of giving us a minimal example of the problem, but I suspect your project is bigger then this example and to give you any advice about good next steps, I'd need a description of the end goal – Jeff Richards Nov 12 '17 at 16:45
  • That makes sense so let's remove containers from the equation (https://stackoverflow.com/questions/47244913/xinetd-service-python-script-doesnt-execute-properly). Xinetd alone may be sufficient for my needs. The reason I want to expose my program over a network is to avoid exposing the code itself via interactive shell. Packaging code in container, exposing a port, and building xinetd service in container behind that port is more or less the same thing as the link I provided, perhaps just with increased scalability. Socket activation could be interesting, user persistence to container too. – Dan Nov 12 '17 at 20:11
  • I had also at your other question, it looks like you are pretty close to what you want, unfortunately I'm noticing sure what the issue there is. Maybe it is something to do with the service settings? Sorry I can't be more help! – Jeff Richards Nov 12 '17 at 22:16
  • No problem Jeff. I did some more testing today, can you have a look at the last comment I left on this post and let me know your opinion? https://stackoverflow.com/questions/47244913/xinetd-service-python-script-doesnt-execute-properly I hope to leave you alone after this. Really appreciate your input. – Dan Nov 14 '17 at 03:14