Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a local file on container?

I'm trying create a container to run a program. I'm using a pre configurate image and now I need run the program. However, it's a machine learning program and I need a dataset from my computer to run.

The file is too large to be copied to the container. It would be best if the program running in the container searched the dataset in a local directory of my computer, but I don't know how I can do this.

Is there any way to do this reference with some docker command? Or using Dockerfile?

like image 983
gomesh Avatar asked Jul 03 '17 01:07

gomesh


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.

Can you copy a file from local to run container?

The Docker cp command can be used to copy files and directories from the host machine to a container and vice-versa. To copy one single file from the host to container, you can use the command below. You can use the container ID as well. To find the name of the container or ID, you can list all the containers.

How do I access files outside the Docker container?

We can useBound mountwithNumber of rolls. There is not much difference between the two, except that Bind Mounts can point to any folder on the host and are not directly managed by Docker. This will map the folder to the logs subfolder in the user's home directory.


1 Answers

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.

For example, if I want to mount a folder from my home directory into /mnt/mydata in a container, I can do:

docker run -v /Users/andy/mydata:/mnt/mydata myimage 

Now, /mnt/mydata inside the container will have access to /Users/andy/mydata on my host.

Keep in mind, if you are using Docker for Mac or Docker for Windows there are specific directories on the host that are allowed by default:

If you are using Docker Machine on Mac or Windows, your Docker Engine daemon has only limited access to your macOS or Windows filesystem. Docker Machine tries to auto-share your /Users (macOS) or C:\Users (Windows) directory. So, you can mount files or directories on macOS using.

Update July 2019:

I've updated the documentation link and naming to be correct. These type of mounts are called "bind mounts". The snippet about Docker for Mac or Windows no longer appears in the documentation but it should still apply. I'm not sure why they removed it (my Docker for Mac still has an explicit list of allowed mounting paths on the host).

like image 137
Andy Shinn Avatar answered Sep 24 '22 21:09

Andy Shinn