How to mount a directory in the docker container to the host?
Asked Answered
E

5

10

It's quite easy to mount a host directory in the docker container.

But I need the other way around.

I use a docker container as a development environment for developing WordPress plugins. This docker container contains everything needed to run WordPress (MySQL, Apache, PHP and WordPress). I mount my plugin src folder from the host in the docker container, so that I can test my plugin during development.

For debugging it would be helpful if my IDE running on the host has read access to the WordPress files in the docker container.

I found two ways to solve the problem but both seem really hacky.

  1. Adding a data volume to the docker container, with the path to the WordPress files

    docker run ... -v /usr/share/wordpress/ ...

    Docker adds this directory to the path on the host /var/lib/docker/vfs/dir... But you need to look up the actual path with docker inspect and you need root access rights to see the files.

  2. Mounting a host directory to the docker container and copying the WordPress files in the container to that mounted host directory. A symlink doesn't seem to work.

Is there a better way to do that? Without copying files or changing access rights?

Thank you!

Enscroll answered 22/1, 2015 at 8:10 Comment(0)
E
1

Copying the WordPress files to the mounted folder was the solution.

I move the files in the container from the original folder to the mounted folder and use symbolic links to link them back to the original folder.

The important part is, the container can follow symbolic links in the container and but the host can't. So just using symbolic links from the original folder to the mounted folder doesn't work, because the host cannot follow symbolic links in the container!

Enscroll answered 22/3, 2015 at 12:59 Comment(1)
Can't we go to the filesystem level i.e. /var/lib/dockerand create a symbolic link from your host folder to the container folder -- as long as you can find it?Trueman
M
0

You can share the files using smb with svendowideits samba container like this:

docker run --rm -v $(which docker):/docker -v /var/run/docker.sock:/docker.sock svendowideit/samba <container name>
Morra answered 25/1, 2015 at 22:7 Comment(0)
M
0

It's possible to do if you use volume instead of filesystem path. It's created for you automatically, if it already doesn't exist.

docker run -d -v usr_share_wordpress:/usr/share/wordpress --name your_container ... image

After you stop or remove your container, your volume will be stored on your filesystem with files from container.

You can inspect volume content during lifetime of your_container with busybox image. Something like:

docker run -it --rm --volumes-from your_container busybox sh

After shutdown of your_container you can still check volume with:

docker run -it --rm -v usr_share_wordpress:/usr/share/wordpress busybox sh

List volumes with docker volume ls.

Moten answered 7/3, 2018 at 10:45 Comment(0)
T
0

I had a similar need of exposing the files from container to the host. There is an open issue on this as of today. One of the work-arounds mentioned, using binds, is pretty neat; it works when the container is up and running:

container_root=$(docker inspect --format {{.State.Pid}} "$container_name")/root
sudo bindfs --map=root/"$USER" "$container_root/$app_folder" "$host_folder"

PS: I am not sure this is good for production, but it should work in development scenarios!

Trueman answered 17/11, 2020 at 15:2 Comment(0)
T
-1

Why not just do: docker run ... -v /usr/share/wordpress/:/usr/share/wordpress. Now your local /usr/share/wordpress/ is mapped to /usr/share/wordpress in the Docker container and both have the same files. You could also mount elsewhere in the container this way. The syntax is host_path:container_path, so if you wanted to mount /usr/share/wordpress from your host to /my/new/path on the container, you'd just do: docker run ... -v /usr/share/wordpress/:/my/new/path.

Talithatalk answered 22/1, 2015 at 9:27 Comment(2)
The local /usr/share/wordpress will replace the contents of /usr/share/wordpress in the container. The local folder is empty and only the container folder contains the WordPress files. So the WordPress files in the container will be replaced by the empty local folder. I need the other way around.Enscroll
@KaiHofstetter You could probably work this around by keeping your original wordpress files some other place than /usr/share/wordpress and copy them there at container startup (usually done by the entrypoint script). This way you will have the files propagated back to the host mapped directory.Glasshouse

© 2022 - 2024 — McMap. All rights reserved.