如何将docker容器中的目录装载到主机? [英] How to mount a directory in the docker container to the host?

查看:155
本文介绍了如何将docker容器中的目录装载到主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是我需要另外一种方法。



我使用docker容器作为开发WordPress插件的开发环境。此Docker容器包含运行WordPress(MySQL,Apache,PHP和WordPress)所需的所有内容。我从Docker容器中的主机挂载了我的插件src文件夹,这样我可以在开发过程中测试我的插件。



对于调试,如果我的IDE运行在主机已经读取了Docker容器中WordPress文件的访问权限。



我发现了两种方法可以解决问题,但两者似乎都很麻烦。


  1. 将数据卷添加到docker容器中,并具有WordPress文件的路径



    docker运行... -v / usr / share / wordpress / ...



    Docker将此目录添加到主机/ var / lib / docker / vfs / dir ...但是您需要查看docker检查的实际路径,并且需要根访问权才能查看文件。


  2. 将主机目录安装到docker容器,并将容器中的WordPress文件复制到已挂接的主机目录。符号链接似乎不起作用。


有更好的方法吗?不复制文件或更改访问权限?



谢谢!

解决方案



我将容器中的文件从原始文件夹移动到已装载的文件夹,并使用符号链接将其链接回原始文件夹。



重要的部分是容器可以跟随容器中的符号链接,但主机不能。因此,只需使用从原始文件夹到挂载文件夹的符号链接不起作用,因为主机无法跟踪容器中的符号链接!


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!

这篇关于如何将docker容器中的目录装载到主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆