What will be told in this article?
Almost any application development touches storing data in a database. In this article I will show you how to connect from dockerized symfony application to most popular MySql or MariaDB server. Also I will prepare video and you can find and watch it on my YouTube channel.
Lets say we decided to build some PHP application using Symfony framework and pack it into the Docker container. On my previous article and video I've showed how to do it, but we go further and we need to connect your database. As MariaDB is in fact a branch of MySQL, - I will show example with MySQL, configuration of Maria will be same.
Normally on localhost the database connection information is stored as an environment variable called DATABASE_URL. This variable can be founded in your .env* file depending on your environment(Symfony ver. 4.4 - 5). How to recognize environment? Here is the explanation:
- .env - default values for the environment variables(git committed file)
- .env.local - local overrides (git uncommitted file)
- .env.$APP_ENV - environment-specific defaults(git committed file)
- .env.$APP_ENV.local - environment-specific overrides(git uncommitted file)
See on screen where you can find it:
Here is schema how DATABASE_URL value can be provided: DATABASE_URL="mysql://db_user:firstname.lastname@example.org:3306/db_name"
As you see in example we have 127.0.0.1 as host and it matches localhost. In fact, here we can provide IP 127.0.0.1 or hostname localhost, both will work BUT it will not work if your database server is outside docker container and lays on same server where docker container is running.
On my video I will tell more what is better to use: host IP or hostname for database configuration.
Do you mean docker container have its own network?
Yes, that's what I want you to say - docker container have its own network and if you put localhost or 127.0.0.1 as database server host - it will look try to find MySQL server on the docker container BUT it will not gonna look for MySQL on your local machine.
For this cases there is special dockerhost. And you may configure it in different ways. One of way is docker-compose.yaml
There you should add:
And restart your container.
Is this the way how to assign external IP for docker container?
We can say so. Docker builds its own networks and are able to control that. Even if you will not add that configuration, in docker-compose.yaml - you still should use dockerhost as pointer for MySQL, located on your local machine. I will tell more about network conflicts and how to avoid it on video, so if you're interested - check my YouTube channel.
Now, lets take a look on another side. MySQL server will receive request not from localhost but from external network and 188.8.131.52 host or whatever you assign. Does MySQL will get connected by default with any external host?
NO! It should be enabled in configuration, both: allow external connections and add external host user for your database.
MySQL configuration for external host connections can be another topic, so if you're interested - leave your comments under video on YouTube and I will prepare it for you!