91

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?

Paolo
  • 16,171
  • 20
  • 78
  • 110
gomesh
  • 1,023
  • 1
  • 6
  • 8

1 Answers1

125

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).

Andy Shinn
  • 19,843
  • 6
  • 62
  • 80
  • Thanks Andy. But when I do this command will the files in the /mnt/mydata folder be accessed normally? Can I do things like cd /mnt/mydata/test? I did what you said, but when I use an ls command inside the mydata directory in the container there is no file. – gomesh Jul 03 '17 at 03:02
  • I think in this case you should ask a new question with your `docker run` command and the expected results since it is now outside the scope of this question. That is indeed how it should work. It sounds like maybe the host folder is incorrect or you are mounting something from outside of `/Users`. Be sure to include versions of Docker and which Docker (Docker for Windows, etc) you are using. – Andy Shinn Jul 03 '17 at 03:08
  • 3
    I think you will get an error unless you put the image name before the command: `docker myimage run -v /Users/andy/mydata:/mnt/mydata` – Monica Heddneck Nov 16 '18 at 22:25
  • @MonicaHeddneck from `docker run --help` : `Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]` – Théo Rubenach Feb 25 '20 at 16:35
  • @AndyShinn I think you was the person I was looking for, please check this question: https://security.stackexchange.com/questions/237987/docker-not-able-to-read-local-file-system As you said this is some kind of Permission related thing, How can we overcome from this? Please answer on that question. – janu agrawal Sep 18 '20 at 03:25