简体   繁体   English

在 docker-compose 中使用卷时删除 Mariadb 表

[英]Mariadb tables are deleted when use volume in docker-compose

I'm copy previous mariadb container's data(/var/lib/mysql) and paste data to new container image.我正在复制以前的 mariadb 容器的数据(/var/lib/mysql)并将数据粘贴到新的容器映像中。 this is Dockerfile这是 Dockerfile

FROM mariadb:latest

ENV MYSQL_ROOT_PASSWORD tt
ENV MYSQL_DATABASE tt
ENV MYSQL_USER tt
ENV MYSQL_PASSWORD tt

# copy other database data
ADD mysql /var/lib/mysql
RUN chown -R mysql:mysql /var/lib/mysql

VOLUME /var/lib/mysql

EXPOSE 3306

CMD ["mysqld"]

when I build docker image, all table remained当我构建 docker 映像时,所有表都保留

but run docker image by using volume, all table disappear, just db.opt remains.但是使用卷运行 docker 映像,所有表都消失了,只剩下 db.opt 。

how can i get database's data with using volume?如何使用卷获取数据库的数据?

Your problem it's related on how VOLUMES work, what you are doing it's adding stuff inside /var/lib/mysql at build time, and then when you run the container, a new EMPTY VOLUME is created and linked at the /var/lib/mysql, which pretty much overwrites anything you put there before.您的问题与 VOLUMES 的工作方式有关,您正在做什么是在构建时在 /var/lib/mysql 中添加内容,然后当您运行容器时,会创建一个新的 EMPTY VOLUME 并将其链接到 /var/lib/ mysql,它几乎会覆盖你之前放在那里的任何东西。

The correct way to go about it for an existing table space would be to create a volume with the docker volume create syntax (ref: https://docs.docker.com/storage/volumes/ ) and then using this trick ( https://github.com/moby/moby/issues/25245#issuecomment-365980572 ) you can add the table data to your volume, and then you run your mariadb container mounting said volume docker run --mount source=myvolume,target=/var/lib/mysql mariadb:latest The correct way to go about it for an existing table space would be to create a volume with the docker volume create syntax (ref: https://docs.docker.com/storage/volumes/ ) and then using this trick ( https: //github.com/moby/moby/issues/25245#issuecomment-365980572 )您可以将表数据添加到您的卷中,然后运行您的 mariadb 容器安装该卷docker run --mount source=myvolume,target=/var/lib/mysql mariadb:latest

I will add that you shouldn't build an image with the tables added at the docker build layer, it makes the docker image huge and it's a wrong use of it all around except for some niche cases like QA databases that you destroy afterwards or read-only databases.我要补充一点,您不应该使用在 docker 构建层添加的表来构建图像,它会使 docker 图像变得巨大,并且除了某些利基案例(例如您之后销毁或读取的 QA 数据库)外,它的所有用途都是错误的- 仅数据库。 The usual way about it it's either using VOLUMES as you stated or with bindings from the host OS.通常的方法是使用您所说的 VOLUMES 或使用来自主机操作系统的绑定。

Then, you shouldn't use mariadb:latest either, choose a particular version and stick with it, as using mariadb:latest can upgrade/downgrade your version and cause all kind of funny bugs.然后,您也不应该使用 mariadb:latest ,选择一个特定的版本并坚持使用它,因为使用 mariadb:latest 可以升级/降级您的版本并导致各种有趣的错误。

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

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