我正在大学从事一个小项目,我想知道是否可以使用 git/github 同步两个 docker 容器。我们在数据库上成对工作,每个人都有一个在 docker 上运行的数据库(两者都具有相同的设置,由相同的 docker-compose 文件启动)。 我们需要用相同的数据填充两个数据库,如果我们可以单独工作并使用 git 或类似的东西同步我们的数据库,那么会更容易。我知道用数据创建一些 sql 脚本会更好/更容易,但我只是好奇。 我考虑过将卷文件复制到 git,并创建一些简单的脚本,从存储库中提取卷并将其复制到现有数据库的卷。
最佳答案
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 与转储集成,这样您就可以在分支上工作并 pull 或推送更改,然后填充数据库。
关于mysql - 可以使用git同步不同机器上的docker容器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929590/