简体   繁体   中英

Possible to run two instances of docker containers on one mysql database container?

There are three containers

Container A : web server Container B : replicate web server of Container A Container Z : mysql datastore container for Container A

Can I run Container A and B at the same time using Container Z as mysql datastore? will it corrupt mysql data store?

Runtime of the containers below:

Container Z : docker run --name mysql_datastore -it busybox:mysql_datastore true

Container A: docker run -it -p 80:80 --volumes-from mysql_datastore --name webservera -h webservera 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.

Can I run multiple mysql daemons in different containers that all share a single data volume?

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

Can I run multiple containers that connect to a single mysql database container?

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:

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

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) 

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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