user3174311
user3174311

Reputation: 1983

How to connect Symfony4 to mysql using docker

I am trying to connect (local development) symfony4 to a db in docker, this is the docker container

83d1b68ce44a        cytopia/mariadb-10.1:latest      "/docker-entrypoint.…"   3 days ago          Up About an hour    0.0.0.0:3306->3306/tcp                       devilbox_mysql_1

web is working and fully configured, I don't understand what host should I use in the .env file.

thanks

Upvotes: 2

Views: 467

Answers (3)

Ilya D
Ilya D

Reputation: 1

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

This example "mariadb" is container alias name.

Upvotes: 0

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10032

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.

Upvotes: 0

A.L
A.L

Reputation: 10483

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:[email protected]: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

Upvotes: 1

Related Questions