简体   繁体   English

如何创建保存 MySQL 容器数据到 docker 图像?

[英]How to create a save MySQL container data to a docker image?

Like the title says, I'm running a mysql container on docker. I've another java application running on another docker container and they can communicate with each other.正如标题所说,我在 docker 上运行一个 mysql 容器。我有另一个 java 应用程序在另一个 docker 容器上运行,它们可以相互通信。 However, each time I have to make some tweaks to my mysql container, I've to rerun it and the data is lost.但是,每次我必须对我的 mysql 容器进行一些调整时,我必须重新运行它并且数据丢失了。 Is there any way for me to commit all those data changes to another docker image so that I can reuse that particular image to create a mysql container having all the data?我有什么办法可以将所有这些数据更改提交到另一个 docker 图像,以便我可以重用该特定图像来创建一个包含所有数据的 mysql 容器? Thanks谢谢

You will need to create a docker volume to maintain that information.您将需要创建一个 docker 卷来维护该信息。

From the doc文档

Create a data directory on a suitable volume on your host system, eg /my/own/datadir.在主机系统的合适卷上创建数据目录,例如 /my/own/datadir。

Start your mysql container like this:像这样启动 mysql 容器:

$ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag $ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Storage is decoupled from applications when you containerize them.当您容器化应用程序时,存储与应用程序分离。 They are designed this way on purpose so that our containers can be as thin as possible.它们是故意这样设计的,这样我们的容器就可以尽可能薄。

If you need data to persist in between the destruction and recreation of a container, then you will need to make use of a "volume".如果您需要在容器的销毁和重建之间保留数据,那么您将需要使用“卷”。

In ELI5 terms, a volume is basically a flash drive for a container.在 ELI5 术语中,卷基本上是容器的 flash 驱动器。 You store it separately, and connect containers to it through a mount point.您单独存储它,并通过挂载点将容器连接到它。 You then instruct the applications in your containers to write data to that mount point -- that way the data is stored externally to your actual container.然后,您指示容器中的应用程序将数据写入该安装点——这样数据就存储在实际容器的外部。

More information can be found here: https://docs.docker.com/storage/volumes/可以在这里找到更多信息: https://docs.docker.com/storage/volumes/

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

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