简体   繁体   English

Docker卷:在远程主机上保留数据

[英]Docker volume: persist data on a remote host

https://docs.docker.com/storage/#more-details-about-mount-types https://docs.docker.com/storage/#more-details-about-mount-types

Good use cases for volumes 卷的好用例

  • When you want to store your container's data on a remote host or a cloud provider, rather than locally. 如果要将容器的数据存储在远程主机或云提供程序上,而不是本地存储。

How is this accomplished with docker volume? 这是如何用docker音量完成的? Aren't docker volume under hosts's /var/lib/docker? 主机的/ var / lib / docker下不是docker卷吗?

Could you give me an example of "docker volume create" of this and how it could be utilized? 你能给我一个关于“码头量创造”的例子以及如何利用它吗?

Yes, volumes are created under /var/lib/docker/volumes/ so you need to link this volume with the folder you want to persist or where you have your data to persist. 是的,卷是在/ var / lib / docker / volumes /下创建的,因此您需要将此卷与要保留的文件夹或要保留数据的位置相关联。

Example: 例:

You have your image named ImageExample and your project under /var/www/MyProject/. 您的图像名为ImageExample,项目位于/ var / www / MyProject /下。

First, you need to create new volume and assign a name. 首先,您需要创建新卷并指定名称。

$ docker volume create --name VolumeExample

# if you run: docker volume ls, they list all your volumes available

$ docker volume ls
DRIVER              VOLUME NAME
local               JbpmVolume1
local               VolumeExample

Second, you have to link your new volume to a folder in your container. 其次,您必须将新卷链接到容器中的文件夹。

$ docker run -v VolumeExample:/var/www/MyProject/ -p 8080:8080 MyImage

Where run is the command to create the container, -p is to map the local and host ports, MyImage is the image used in this example, VolumeExample is the volume created before and /var/www/MyProject/ is the example folder which you need to persist. 其中run是创建容器的命令,-p是映射本地端口和主机端口,MyImage是本例中使用的映像,VolumeExample是之前创建的卷,而/ var / www / MyProject /是你的示例文件夹需要坚持下去。

You can use this volume to store application configuration, database data or configuration too and so on. 您也可以使用此卷来存储应用程序配置,数据库数据或配置等。 Maybe, depends on what you need to store, you can use bind mount or volumes or if your host is in linux, you can use tmpfs mounts. 也许,取决于你需要存储什么,你可以使用bind mount或卷,或者如果你的主机在linux中,你可以使用tmpfs mounts。

As simple as that, you can read more about in docker webpage but basically this is how to work with a volume. 就这么简单,您可以在docker网页上阅读更多相关内容,但基本上这是如何使用卷。 Every time you stop/start or create/delete the container, the data in your volume will persist. 每次停止/启动或创建/删除容器时,卷中的数据都将保留。

I do it in this way, because this is not the "happy path" you want. 我是这样做的,因为这不是你想要的“快乐路径”。 You have to mount before you store the data in the folder, because when you mount the volume, the folder will be empty because the volume is empty. 在将数据存储到文件夹之前必须先装入,因为在装入卷时,该文件夹将为空,因为该卷为空。 If you have data in the folder before you mount the volume, the data will be not visible for you. 如果在装入卷之前文件夹中有数据,则数据将不可见。 So it depends on your project the way you will create the volume, but basically, with this two commands you mount the volume into the host container. 因此,它取决于您的项目创建卷的方式,但基本上,使用这两个命令将卷装入主机容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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