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

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

问题描述

假设我有一个带有这个简单 Dockerfile 的应用程序:

Assume that i have an application with this simple Dockerfile:

//...
RUN configure.sh --logmyfiles /var/lib/myapp
ENTRYPOINT ["starter.sh"]
CMD ["run"]
EXPOSE 8080
VOLUME ["/var/lib/myapp"]

然后我从中运行一个容器:

And I run a container from that:

sudo docker run -d --name myapp -p 8080:8080 myapp:latest

所以它可以正常工作并将一些日志存储在 docker 容器的 /var/lib/myapp 中.

So it works properly and stores some logs in /var/lib/myapp of docker container.

我的问题

我也需要这些日志文件自动保存在主机中,那么如何将容器中的 /var/lib/myapp 挂载到 /var/lib/myapp主机服务器中的代码>(不删除当前容器)?

I need these log files to automatically saved in host too, So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server (without removing current container) ?

编辑

我还看到 Docker - 从容器挂载目录到主机,但这并不能解决我的问题,我需要一种方法将我的文件从 docker 备份到主机.

I also see Docker - Mount Directory From Container to Host, but it doesn't solve my problem i need a way to backup my files from docker to host.

推荐答案

首先,关于Docker 卷.卷装载仅在容器创建时发生.这意味着您在启动容器后无法更改卷安装.此外,卷挂载只有一种方式:从主机到容器,反之亦然.当您将主机目录指定为容器中的卷时(例如: docker run -d --name="foo" -v "/path/on/host:/path/on/container"ubuntu),它是一个regular ole"linux mount --bind,这意味着宿主目录将暂时覆盖"容器目录.目标目录上实际上没有任何内容被删除或覆盖,但由于容器的性质,这实际上意味着它将在容器的生命周期内被覆盖.

First, a little information about Docker volumes. Volume mounts occur only at container creation time. That means you cannot change volume mounts after you've started the container. Also, volume mounts are one-way only: From the host to the container, and not vice-versa. When you specify a host directory mounted as a volume in your container (for example something like: docker run -d --name="foo" -v "/path/on/host:/path/on/container" ubuntu), it is a "regular ole" linux mount --bind, which means that the host directory will temporarily "override" the container directory. Nothing is actually deleted or overwritten on the destination directory, but because of the nature of containers, that effectively means it will be overridden for the lifetime of the container.

所以,您有两个选择(也许三个).您可以将主机目录挂载到容器中,然后将这些文件复制到启动脚本中(或者如果您 将 cron 带入您的容器,您可以使用 cron 定期将这些文件复制到该主机目录卷挂载).

So, you're left with two options (maybe three). You could mount a host directory into your container and then copy those files in your startup script (or if you bring cron into your container, you could use a cron to periodically copy those files to that host directory volume mount).

您还可以使用 docker cp 将文件从容器移动到主机.现在这有点老套,绝对不应该在基础设施自动化中使用.但它确实能很好地达到这个目的.一次性或调试是一种很好的情况.

You could also use docker cp to move files from your container to your host. Now that is kinda hacky and definitely not something you should use in your infrastructure automation. But it does work very well for that exact purpose. One-off or debugging is a great situation for that.

您也可以设置网络传输,但这与您正在做的事情有关.但是,如果您想定期为您的日志文件(或其他)执行此操作,您可以考虑使用诸如 rsyslog 之类的东西将这些文件移出您的容器.

You could also possibly set up a network transfer, but that's pretty involved for what you're doing. However, if you want to do this regularly for your log files (or whatever), you could look into using something like rsyslog to move those files off your container.

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

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