I’ve got my dev environment running in docker (nginx, php and mariadb) and try to create a database in symfony with doctrine. When I run php bin/console doctrine:database:create
, I will get the following error:
[critical] Error thrown while running command "doctrine:database:create". Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution"
My configuration in .env
looks like this:
DATABASE_URL="mysql://admin:symfony-admin@db/symfony_test?serverVersion=mariadb-10.1"
And this is my docker-compose.yml
:
version: "3.6"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
php:
build: .
volumes:
- ./code:/code
links:
- db
db:
image: mariadb:latest
restart: always
ports:
- "33006:3306"
volumes:
- ./db:/docker-entrypoint-initdb.d/
environment:
MYSQL_ROOT_PASSWORD: 'symfony-root-pwd'
MYSQL_DATABASE: 'symfony_db'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'symfony-admin'
I also use adminer to have access to the database and there the login works.
Does someone know why I can’t create a database with doctrine?
Cheers,
Michael
Solution:
I found the solution by myself. The command php bin/console doctrine:database:create
need to be run within the php docker container and not in the local terminal.
So at first docker-compose exec php /bin/bash
and then php bin/console doctrine:database:create
2
Answers
You can add
to
php
service config and change yourDATABASE_URL
toDATABASE_URL="mysql://admin:symfony-admin@db/symfony_db?serverVersion=mariadb-X.X.X"
Service
db
will be exposed tophp
service, and inphp
containerdb
will be host (link) todb
serviceUsually, this problem is in the password. As the Connection String is a URL, that encodes the special characters thinking it is a parameter.
To make it clear, fixing this requires one of these 2 steps:
For example, by replacing character # by %23 (its encoded version).
The first one works for me!
Answer reference: Malformed parameter "url". Symfony 5 does not accept special characters. #35568