简体   繁体   English

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

[英]Can you synchronize docker containers on different machines using git?

I'm working on small project at uni, and I wonder if is it possible to synchronize two docker containers using git/github.我在 uni 从事小型项目,我想知道是否可以使用 git/github 同步两个 docker 容器。 We are working in pairs on the database, and each of us has a database running on docker (both with same settings, started by same docker-compose file).我们在数据库上成对工作,我们每个人都有一个在 docker 上运行的数据库(两者都具有相同的设置,由相同的 docker-compose 文件启动)。 We need to populate both databases with same data, and would be easier if we can work separetly and just sync our db's using git or something like that.我们需要用相同的数据填充两个数据库,如果我们可以单独工作并使用 git 或类似的东西同步我们的数据库,将会更容易。 I know that creating some sql scripts with data would be better/easier but I'm just curious.我知道用数据创建一些 sql 脚本会更好/更容易,但我只是好奇。 I thought about copying volume file to git, and creating some simple script that pull volume from repo and copy it to volume of existing DB.我考虑将卷文件复制到 git,并创建一些简单的脚本从 repo 中提取卷并将其复制到现有数据库的卷。

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

Consider this when working with database data.在处理数据库数据时考虑这一点。 You can integrate git with the dumps so you can work on branches and pull or push the changes and then just populate the database.您可以将 git 与转储集成,这样您就可以在分支上工作并拉取或推送更改,然后只需填充数据库。

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

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