-1

I hope I can explain my question as clear as possible. I created a Docker image including a python script, in this script the user is asked to give the path and name of the file one wishes to manipulate.

This is my Dockerfile:

FROM python:2.7.16

ADD my_script.py /

RUN pip install Pillow
RUN pip install numpy ==1.16.2

CMD [ "python", "./my_script.py" ]

Since one cannot access files in the host system from the container, I figured it is best to create a volume to share files between the host and the container and I saved some files to try my script in the 'host-data' folder, so I did:

docker run -d -P --name script-container -v ~/host-data/:container-data my_image

It works until one gives the path and name of the file and I keep getting this error message

Traceback (most recent call last):
  File "./my_script.py", line 14, in <module>
    image = Image.open("path+name")
  File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2766, in open
Path: File name:     fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'path+name'

I am relatively new to Docker so I'd really appreciate some help.

This is the portion of the code where the error is occurring.

from __future__ import division 
from PIL import Image
import numpy as np 
path = str(raw_input("path to the file ")) 
name = str(raw_input("name of the file ")) 
image = Image.open(path + nombre)
Azsgy
  • 3,139
  • 2
  • 29
  • 40
DatGuy
  • 377
  • 1
  • 4
  • 10
  • In the error log, it specifically says the while executing the script PIL(python imaging library) was unable to find the image. I think fixing the path of the image in the script should solve your issue. – Subhrajyoti Das Nov 17 '19 at 04:44
  • I don't know because my script works when executed independently of Docker – DatGuy Nov 17 '19 at 04:55
  • Can you please provide the code used in your script? If your code is long, please provide the relevant parts, such as the parts causing the error and the parts defining the `path` and `name`. – James Mchugh Nov 17 '19 at 05:02
  • Sure, the error part is something like this: `from __future__ import division from PIL import Image import numpy as np path = str(raw_input("path to the file ")) name = str(raw_input("name of the file ")) image = Image.open(path + nombre)` Of course for python 3 one must change raw_input() for input() – DatGuy Nov 17 '19 at 05:13
  • From the error message , it looks like you've passed the filename as a string of `"path+name"`, getting rid of those quotes should work as that will actually return the full path. – Anirudh Panchangam Nov 17 '19 at 14:55
  • For future posts, if a commenter asks for more info, just edit the question and add it there as it is much easier for people to find and read. Also, the code you provided does not reflect what the error is. The error says that the line causing the error is `image = Image.open("path+name")`, but the code you posted shows `image = Image.open(path + nombre)`. There is an inconsistency here. Have you rebuilt the image since changing the code? – James Mchugh Nov 17 '19 at 18:04

1 Answers1

1

In the example you provided, the logs state that PIL is unable to open the file at "path+name". It looks like you meant to concatenate the variables path and name like so

image = Image.open(path + name)

But instead, you wrapped the concatenation in quotes resulting in the string literal of "path+name". Of course, this is only speculation as I do not know what your code looks like.

I should also point out here that it is usually better practice to use os.path.join to concatenate paths as it is more robust than simply adding them. In which case, your code would become

image = Image.open(os.path.join(path, name))

Also, I see that you are using Python 2.7. If possible, I would recommend transition to Python 3 as Python 2 will reach EOL in 2020.

James Mchugh
  • 994
  • 8
  • 26
  • @user215051 Did the answer work for you? If not, how could it be adjusted to better answer your question? If it did, would you mind accepting it? Not only does this allow you to give back to the answerers for taking the time to answer your question, but it also serves to better the SO community by showing users the correct resolution for the issue you faced. This can be a great asset for other users facing the same issue. – James Mchugh Nov 25 '19 at 12:47