如何在Linux主机上的Docker容器内挂载目录 [英] How to mount a directory inside a docker container on Linux host

查看:943
本文介绍了如何在Linux主机上的Docker容器内挂载目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将目录从docker容器挂载到本地文件系统.该目录是网站根目录,我需要能够使用任何编辑器在本地计算机上对其进行编辑.

I would like to mount a directory from a docker container to the local filesystem. the directory is a website root and I need to be able to edit it my local machine using any editor.

我知道我可以运行docker run -v local_path:container_path,但是这样做只会在容器内创建一个空目录.

I know I can run docker run -v local_path:container_path but doing that it is only creating an empty directory inside the container.

如何在Linux主机上的docker容器内挂载目录?

How can one mount a directory inside a docker container on linux host?

推荐答案

如果您的目标是提供现成的LAMP,则应在Dockerfile中使用VOLUMES声明. VOLUME volume_path_in_container 问题是docker不会挂载文件,因为它们已经在创建卷的路径中存在.您可以按照@ Grif-fin在他的评论中所说的去做,或者修改容器的入口点,以便他在运行时将要公开的文件复制到卷中.

If your goal is to provide a ready to go LAMP, you should use the VOLUMES declaration inside the Dockerfile. VOLUME volume_path_in_container The problem is that docker will not mount the file cause they were already present in the path you are creating the volume on. You can go as @Grif-fin said in his comment or modify the entry point of the container so he copy the file you want to expose to the volume at the run time.

您必须在Dockerfile中使用build COPYADD命令插入数据,以便基本文件将出现在容器中.

You have to insert your datas using the build COPY or ADD command in Dockerfile so the base files will be present in the container.

然后创建一个入口点,该入口点会将文件从COPY路径复制到卷路径.

Then create an entrypoint that will copy file from the COPY path to the volume path.

然后使用-v标签运行容器,就像-v local_path:volume_path_in_container一样.这样,您应该将容器内的文件安装在本地. (至少,这是我添加的内容).

Then run the container using the -v tag and like -v local_path:volume_path_in_container. Like this, you should have the files inside the container mounted on the local. (At least, it was what I add).

在此处找到示例: https://github.com/titouanfreville/Docker/tree /master/ready_to_go_lamp .

它将避免每次都要构建,您可以从确定的图像中提供它.

It will avoid to have to build every time and you can provide it from a definitive image.

为了更好,请添加用户支持,以便您是已挂载文件的所有者(如果您不是root用户).

To be nicer, it would be nice to add an user support so you are owner of the mounted files (if you are not root).

希望对您有用.

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

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