Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount an empty folder in host to a non-empty folder in Docker

Tags:

docker

The tool I'm using is delivered in a Docker image. Since installing the tool is extremely complicated with a bunch of dependencies, I want to work on the host with my IDE, but run it on the container.

So after download and load the image, I run:

sudo docker run -it -v /home/myself/WIP/thetool:/home/thetool name/label

Without mounting, the tool is located under /home/thetool, but with mounting, this folder is empty (since the folder in the host is empty).

Do I need to copy the tool from the container, then mount it, or there is a way to do it directly.

like image 342
sean Avatar asked Nov 27 '17 20:11

sean


People also ask

How do I mount a folder inside a docker container?

How to Mount Local Directories using docker run -v. Using the parameter -v allows you to bind a local directory. -v or --volume allows you to mount local directories and files to your container. For example, you can start a MySQL database and mount the data directory to store the actual data in your mounted directory.

Can I mount a file in docker?

The Docker CLI provides the –mount and –volume options with a run command to bind a single file or directory. Both flags work similarly but have different syntaxes. As a result, we can use them interchangeably.

What is a bind mount docker?

A Bind Mount is a storage area (file/directory) on your local machine available inside your container. So any changes you make to this storage space (file/directory) from the outside container will be reflected inside the docker container and vice-versa.


1 Answers

You can actually make that work with a docker volume with explicit device mount point.

If the directory /home/myself/WIP/thetool is empty, do the following:

Create a docker volume as such:


docker volume create --driver local \
    --opt type=none \
    --opt device=/home/myself/WIP/thetool \
    --opt o=bind \
    tool_vol

Start the container and mount the created volume:

sudo docker run -it -v tool_vol:/home/thetool name/label

Now the data that is in /home/thetool inside the container will be available inside /home/myself/WIP/thetool even though the host folder was initially empty.

like image 104
yamenk Avatar answered Sep 28 '22 03:09

yamenk