简体   繁体   中英

How to mount a directory in the docker container to the host?

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!

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!

您可以使用 smb 与svendowideits samba 容器共享文件,如下所示:

docker run --rm -v $(which docker):/docker -v /var/run/docker.sock:/docker.sock svendowideit/samba <container name>

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 .

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM