可以在一个mysql数据库容器上运行docker容器的两个实例吗? [英] Possible to run two instances of docker containers on one mysql database container?
问题描述
有三个容器
容器A:Web服务器 容器B:容器A的复制Web服务器 容器Z:容器A的mysql数据存储容器
Container A : web server Container B : replicate web server of Container A Container Z : mysql datastore container for Container A
我可以同时使用容器Z作为mysql数据存储运行容器A和B吗?会损坏mysql数据存储吗?
Can I run Container A and B at the same time using Container Z as mysql datastore? will it corrupt mysql data store?
以下容器的运行时:
容器Z: docker run --name mysql_datastore -it busybox:mysql_datastore true
Container Z : docker run --name mysql_datastore -it busybox:mysql_datastore true
容器A: docker run -it -p 80:80 --volumes- from mysql_datastore --name webservera -h webservera centos:webseverwithmysql/bin/bash
Container A: docker run -it -p 80:80 --volumes-from mysql_datastore --name webservera -h webservera centos:webseverwithmysql /bin/bash
容器B: docker run -it -p 81:81 --volumes- from mysql_datastore --name webserverb -h webserverb centos:webseverwithmysql/bin/bash
Container B : docker run -it -p 81:81 --volumes-from mysql_datastore --name webserverb -h webserverb centos:webseverwithmysql /bin/bash
推荐答案
希望这些解释之一是正确的.
Hopefully one of these interpretations is correct.
否,每个守护程序都需要一个单独的数据目录以避免冲突.您可以在共享卷中放置多个数据目录,但是这样做的结果是多个完全独立的数据库. -源
No, each daemon needs a separate data directory to avoid conflicts. You could put multiple data directories in the shared volume, but the result of that is multiple completely separate databases. - source
是的,可以允许多个容器连接到单个数据库容器,但不能共享卷.容器Z将运行mysql守护程序,其他容器可以通过tcp套接字连接到它. 官方mysql存储库自述文件具有入门步骤:
Yes it is possible to allow multiple containers to connect to a single database container, but not by sharing volumes. Container Z will run the mysql daemon and other containers can connect to it via tcp sockets. The official mysql repo readme has steps to get started:
首先启动ContainerZ.
First start Container Z.
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
然后使用以下类似方式运行要连接到数据库的其他容器:
Then run other containers that you want to connect to the database with something like this:
docker run --name webservera --link some-mysql:mysql -d application-that-uses-mysql
--link
标志的文档.容器链接为链接别名添加了一个主机文件条目,因此您无需手动查找地址.您的网络服务器的数据库配置看起来像这样
Docs for the --link
flag. Container linking adds a hostfile entry for the link alias so you don't have to find the address manually. Your webserver's database configuration would look something like this
jdbc:mysql://address=(protocol=tcp)(host=mysql)(port=3306)(user=root)(password=mysecretpassword)
我希望这会有所帮助.
这篇关于可以在一个mysql数据库容器上运行docker容器的两个实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!