accessing docker database from other docker container(s)
This is a very confusing matter to me.
I have multiple containers, each comes with it’s own database, which creates resources overhead.
I’ve deployed a mariadb container and I want the other containers to use it, but it appears something is always going wrong and I can’t figure it out.
So here are some notes:
- When deploying the mariadb I configured it to be on the same docker network as the other services.
- when deploying mariadb I used bind volume /a/path:/config … but I can’t find folder called config on that path, I can only find a log/ , databases/ and custom.cnf … should I create the /config directory myself or is it the same as databases?
- inside the /databases there are /maria_db and /mysql … where are the databases kept? NOTE that I called the database environment variable as maria_db .. so is the databases saved here?
- The directory /databases/mysql/ and /databases/maria_db/ both have files that seems to me as databases (example .frm , .ibd) but the /mysql has more file extensions that I can’t find in /maria_db, (example .MAD , .MAI) and that is confusing ( see next note).
- If I have to let the other containers use this DB, should I just make environment variables that calls the database (example: MYSQL_DATABASE=maria_db) or should I also give the other dockers access to the databases directory? and if so, which one?, /mariadb ,, /mysql ,, or the parent folder /databases that has both?
- For each docker I want to give a different database username and pass (MYSQL_USER , MYSQL_PASSWORD) is this a good idea?? ( what I know I should add them to the database by console not passing them with variables, except for the first username and password)
- If I should mount the database directory to each docker that will use it, where should I mount it on the docker that will use it?
It’s a lot of questions I know, but it’s a confusing topic that I couldn’t figure out myself, even that I spent hours and hours reading (yesterday alone I spent 12 hours on this).
Thanks in advance.
You seem to have a minor misconception about accessing databases. No application other than the database itself should be modifying raw database files, and all other applications should interface with the database in order to access or modify information.
2: A large majority of databases as docker images follow a convention, that is passing configuration variables through environment variables. Refer here: https://mariadb.com/kb/en/installing-and-using-mariadb-via-docker/
When you attempt to bind mount /a/path:/config
, you are trying to bind mount /a/path
on your local system to /config
in the docker container. To my knowledge, this directory most certainly does not exist in the MariaDB docker.
3: Where are the database data files kept? According to MariaDB, the default location is /var/lib/mysql
.
5: You can pass them as environment variables as mentioned in #2. I do not understand what you mean by a "good idea", but it is certainly doable, and is standard.
7: As mentioned before, you should not.