Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker read file from local system when container is running

Tags:

docker

My Dockerfile runs a python script that requires reading in a text file from my local machine.

Dockerfile

FROM fedeg/python-vim:2
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
COPY file.txt /data/file.txt
CMD ["python", "run.py"]

In the run.py there is a function that reads in the text file:

def read_file(filename):
    lines = [line.rstrip('\n') for line in open(filename)]
    return lines

I was wondering if i were to pass this image to someone else and they run it, will the file.txt already be saved inside the image or will docker be able to look for that file.txt in that person's local machine when the image is started?

What i want is for docker to look for the name file.txt on any machines where the image is running on so it can work on the contents of the other file.txt instead of the file.txt i provided from my local machine.

like image 476
jxn Avatar asked Apr 14 '17 18:04

jxn


People also ask

Can a docker container access local files?

Yes, you can do this. What you are describing is a bind mount. See https://docs.docker.com/storage/bind-mounts/ for documentation on the subject. Now, /mnt/mydata inside the container will have access to /Users/andy/mydata on my host.

How do I access files outside the docker container?

We can do so using Bind Mounts and Volumes. There's not a lot of difference between the two, except Bind Mounts can point to any folder on the host computer, and are not managed by Docker directly. This will map that folder to the logs subfolder in the user's home directory.


2 Answers

As @herm pointed out by building the container the way you did, the file.txt is stored inside the container.

To achieve what you want, remove the COPY from the Dockerfile and instead mount the folder containing file.txt on the host machine with the -v option at run time:

docker run -v /folder/with/file:/data image_name

where /folder/with/file is the path to the folder containing file.txt and image_name is the name of your docker container.

The content of /data in the running container will now be the content of the /folder/with/file folder.

EDIT:

You could also mount just the file instead:

docker run -v /folder/with/file/file.txt:/data/file.txt image_name

However note that this might give problems if you need to edit the file. See Docker Docs "Mount a host file as a data volume"

Note: Many tools used to edit files including vi and sed --in-place may result in an inode change. Since Docker v1.1.0, this will produce an error such as “sed: cannot rename ./sedKdJ9Dy: Device or resource busy”. In the case where you want to edit the mounted file, it is often easiest to instead mount the parent directory.

like image 160
saxitoxin Avatar answered Jan 18 '23 07:01

saxitoxin


Yes it is saved inside the image.

An image contains all the change you describe in the dockerfile. Anything that happens in there is part of the image and will be usable by someone you give the image to.

Anything that happens on your container which you create with docker run or docker service does not effect your image. If you wanted that you could docker commit a container to turn it into a new image.

like image 31
herm Avatar answered Jan 18 '23 06:01

herm