4

I have an issue that my python program doesen't find a given folder in ubuntu with in a docker container.

First I build my docker container and then I run it which goes without problems until my programm don't find a file. I'm working on a Raspberry pi with Ubuntu Core 16 and Docker to start my python file.

I have found a similar question here and tried their solution:

with open(os.path.join(os.path.expanduser('~'), 'SearchFiles', 'data.csv'), 'r') as csvfile:

But now I get the error:

No such File or directory: 'root/Searchfiles/data.csv'

But the program is the folder ~/usr/git/MVP-Project/Searchfiles/data.csv

Dockerfile for starting the Image:

FROM python:3.6
ADD app.py /
RUN pip install numpy
RUN pip install requests
RUN pip install fake_useragent
RUN pip install datetime
RUN pip install selenium
RUN pip install requests_html
CMD [ "python", "./app.py" ]

So why is it showing the wrong path and how to add the correct path ?

Matthis Kohli
  • 1,877
  • 1
  • 21
  • 23
FoldFence
  • 2,674
  • 4
  • 33
  • 57
  • Just to see if I understand your situation: It seems like `os.path.expanduser('~')` is not giving the correct result, because the path should start with a slash `/`. Is that right? – Ralf Nov 02 '18 at 10:30
  • Yeah right and it starts with /root/ and I don't why it starts with 'root', first I tried without os and only the argument 'Searchfiles/data.csv' which isn't working too. I don't know how to find the right path and if docker effects the path structure. – FoldFence Nov 02 '18 at 10:38
  • Docker containers have their own folder structure. You need to put the csv file in the container using the Dockerfile OR map a docker container folder to an OS folder and put the file in there (can be done when you docker run). To explore the insides of the docker container, start it then do docker exec -it [container id] bash. Hope this helps. – Namyts Nov 02 '18 at 10:46
  • I starting my Image with a Dockerfile but I´m new to docker, how can I map these Folders correct ? – FoldFence Nov 02 '18 at 11:17
  • 1
    `os.path.expanduser('~')` returns `/root` for me in the Docker `python:3.6` image (and in the newest `python` image, too). Are you sure you transcribed the output correctly? – tripleee Nov 02 '18 at 12:05
  • If you know in which directory the app runs at all times, because you are controlling it from Docker, you can simply hardcode the path, or use a relative path. I would choose the latter, to make it easy to run inside or outside of Docker. – tripleee Nov 02 '18 at 12:07
  • Sorry, you didn't tag me which means I only saw your reply now. Add -v [main os directory]:[container directory] to your run command (without the []). then you put the csv in the main os directory, and it will be visible to python inside the docker container (in the container directory). – Namyts Nov 02 '18 at 14:34

1 Answers1

4

Your data.csv doesn't exist in your Docker Container because you only copy app.py.

ADD app.py /

Move your data.csv to the same directory as app.py and change the command to.

COPY ["data.csv", "app.py", "/"]

If that didn't work try.

COPY . .

With this approach every file of the directory is available inside of your container and therefor you data.csv has to be there. Well as long as you kept it in the same directory.

Matthis Kohli
  • 1,877
  • 1
  • 21
  • 23