How to connect Symfony4 to mysql using docker How to connect Symfony4 to mysql using docker docker docker

How to connect Symfony4 to mysql using docker


You binded the port 3306 to the host, so we have to tell Symfony to use this port. We have to use the address 127.0.0.1 because with localhost, MySQL try to connect through a socket.

So you should end up with something like this:

DATABASE_URL=mysql://USER:PASSWORD@127.0.0.1:3306/DATABASE_NAME

You may need to configure the access to MariaDB:

doctrine:    dbal:        # configure these for your database server        driver: 'pdo_mysql'        server_version: 'mariadb-10.2'

See MariaDB 10.0 JSON type with symfony 4


Depending the usage you should try different approaches. Id you are NOT using a docker_compose.yml then try with hostname 0.0.0.0:3306 but in case of a dockerfile for example this (It is for moodle but gets the job done).

So in the latter case I use the service name as host name in order to get the job done. Usually I use the docker-compose.yml approach in order to configure my running environment.

Also keep in mind using docker-compose.yml also users a .env file for configuration thus it may cause a conflict as well.


DATABASE_URL=mysql://user:password@mariadb:3306/databaseName

This example "mariadb" is container alias name.