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 ?

7
  • 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? Commented Nov 2, 2018 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. Commented Nov 2, 2018 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. Commented Nov 2, 2018 at 10:46
  • I starting my Image with a Dockerfile but I´m new to docker, how can I map these Folders correct ? Commented Nov 2, 2018 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? Commented Nov 2, 2018 at 12:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If app.py is the program which generates or fetches this data in the first place, the OP can't do that.
If app.py generates or fetches this data he just has to create data.csv and place into the root folder.
After COPY I still get the '/' before the filename which I assume is the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.