您可以使用git在不同机器上同步docker容器吗?

时间:2019-11-19 08:29:13

标签: mysql git docker docker-compose

我正在uni上做一个小项目,我想知道是否可以使用git / github同步两个docker容器。我们正在数据库上成对工作,我们每个人都有一个在docker上运行的数据库(两者具有相同的设置,由相同的docker-compose文件启动)。 我们需要用相同的数据填充两个数据库,如果我们可以分开工作并且仅使用git或类似的方式同步数据库,则将更加容易。我知道用数据创建一些sql脚本会更好/更轻松,但是我很好奇。 我考虑过将卷文件复制到git,并创建一些简单的脚本来从回购中提取卷并将其复制到现有数据库的卷中。

1 个答案:

答案 0 :(得分:1)

Creating database dumps

Most of the normal tools will work, although their usage might be a little convoluted in some cases to ensure they have access to the mysqld server. A simple way to ensure this is to use docker exec and run the tool from the same container, similar to the following:

$ docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

Restoring data from dump files

For restoring data. You can use docker exec command with -i flag, similar to the following:

$ docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-data

@source: dockerhub

在处理数据库数据时请考虑这一点。您可以将git与转储集成在一起,以便可以在分支上工作并拉出或推送更改,然后只填充数据库。