I try to create an API with Symfony and Doctrine with a docker setup.
But when I want to create the database I got the following error:
php bin/console doctrine:database:create
Could not create database `account_analytic` for connection named default
An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for database failed: Temporary failure in name resolution
My docker-compose file looks like this:
version: "3.4"
services:
database:
container_name: database
image: mariadb:10.6
environment:
MYSQL_ROOT_PASSWORD: 'account-analytic-admin-pw'
MYSQL_DATABASE: 'accountAnalytic'
MYSQL_USER: 'account-analytic'
MYSQL_PASSWORD: 'account-analytic-user-pw'
volumes:
- ./data/mariadb:/docker-entrypoint-initdb.d
ports:
- "33006:3306"
networks:
account:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "8081:8080"
container_name: account_analytic_frontend
volumes:
- ./frontend:/usr/src/app/account_analytic_frontend
depends_on:
- database
networks:
account:
php:
container_name: php
build:
context: ./backend
volumes:
- ./backend:/var/www/html
depends_on:
- database
networks:
account:
nginx:
container_name: nginx
image: nginx:stable-alpine
ports:
- '8080:80'
volumes:
- ./backend:/var/www/html
- ./backend/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
account:
networks:
account:
I updated my .env
file to this:
DATABASE_URL="mysql://root:account-analytic-admin-pw@database:3306/account_analytic?serverVersion=10.6"
When I change the URL to mysql://root:account-analytic-admin-pw@127.0.0.1:33006/account_analytic?serverVersion=10.6
it is possible to create the database.
Also it works if I add a Controller and Repository and update the database with php bin/console doctrine:schema:update --dump-sql
and php bin/console doctrine:schema:update --force
.
In this case the table will be inserted into the database with the correct field.
I also can see the correct Endpoints in the API Plattform UI. But if I want to get the entries of the database with the pre defined GET-Endpoint, I got the following error:
{
"@id": "/api/errors/500",
"@type": "hydra:Error",
"title": "An error occurred",
"detail": "An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused",
"status": 500,
"type": "/errors/500",
"trace": [
{
"file": "/var/www/html/vendor/doctrine/dbal/src/Connection.php",
"line": 1943,
"function": "convert",
"class": "Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/dbal/src/Connection.php",
"line": 1891,
"function": "handleDriverException",
"class": "Doctrine\DBAL\Connection",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/dbal/src/Connection.php",
"line": 380,
"function": "convertException",
"class": "Doctrine\DBAL\Connection",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/dbal/src/Connection.php",
"line": 1659,
"function": "connect",
"class": "Doctrine\DBAL\Connection",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/dbal/src/Connection.php",
"line": 1082,
"function": "getWrappedConnection",
"class": "Doctrine\DBAL\Connection",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/Query/Exec/SingleSelectExecutor.php",
"line": 29,
"function": "executeQuery",
"class": "Doctrine\DBAL\Connection",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/Query.php",
"line": 284,
"function": "execute",
"class": "Doctrine\ORM\Query\Exec\SingleSelectExecutor",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/AbstractQuery.php",
"line": 935,
"function": "_doExecute",
"class": "Doctrine\ORM\Query",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/AbstractQuery.php",
"line": 891,
"function": "executeIgnoreQueryCache",
"class": "Doctrine\ORM\AbstractQuery",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/AbstractQuery.php",
"line": 725,
"function": "execute",
"class": "Doctrine\ORM\AbstractQuery",
"type": "->"
},
{
"file": "/var/www/html/vendor/doctrine/orm/src/Tools/Pagination/Paginator.php",
"line": 96,
"function": "getScalarResult",
"class": "Doctrine\ORM\AbstractQuery",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Doctrine/Orm/Paginator.php",
"line": 45,
"function": "count",
"class": "Doctrine\ORM\Tools\Pagination\Paginator",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Hydra/Serializer/CollectionNormalizer.php",
"line": 68,
"function": "getTotalItems",
"class": "ApiPlatform\Doctrine\Orm\Paginator",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Serializer/AbstractCollectionNormalizer.php",
"line": 103,
"function": "getPaginationData",
"class": "ApiPlatform\Hydra\Serializer\CollectionNormalizer",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Hydra/Serializer/PartialCollectionViewNormalizer.php",
"line": 50,
"function": "normalize",
"class": "ApiPlatform\Serializer\AbstractCollectionNormalizer",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Hydra/Serializer/CollectionFiltersNormalizer.php",
"line": 91,
"function": "normalize",
"class": "ApiPlatform\Hydra\Serializer\PartialCollectionViewNormalizer",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/serializer/Serializer.php",
"line": 150,
"function": "normalize",
"class": "ApiPlatform\Hydra\Serializer\CollectionFiltersNormalizer",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/serializer/Serializer.php",
"line": 129,
"function": "normalize",
"class": "Symfony\Component\Serializer\Serializer",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/State/Processor/SerializeProcessor.php",
"line": 73,
"function": "serialize",
"class": "Symfony\Component\Serializer\Serializer",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/State/Processor/WriteProcessor.php",
"line": 51,
"function": "process",
"class": "ApiPlatform\State\Processor\SerializeProcessor",
"type": "->"
},
{
"file": "/var/www/html/vendor/api-platform/core/src/Symfony/Controller/MainController.php",
"line": 111,
"function": "process",
"class": "ApiPlatform\State\Processor\WriteProcessor",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php",
"line": 178,
"function": "__invoke",
"class": "ApiPlatform\Symfony\Controller\MainController",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php",
"line": 76,
"function": "handleRaw",
"class": "Symfony\Component\HttpKernel\HttpKernel",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/http-kernel/Kernel.php",
"line": 185,
"function": "handle",
"class": "Symfony\Component\HttpKernel\HttpKernel",
"type": "->"
},
{
"file": "/var/www/html/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php",
"line": 35,
"function": "handle",
"class": "Symfony\Component\HttpKernel\Kernel",
"type": "->"
},
{
"file": "/var/www/html/vendor/autoload_runtime.php",
"line": 29,
"function": "run",
"class": "Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner",
"type": "->"
},
{
"file": "/var/www/html/public/index.php",
"line": 5,
"function": "require_once"
}
],
"hydra:title": "An error occurred",
"hydra:description": "An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused"
}
For this case I read that I have to change the DATABASE_URL like I did above with the correct docker container name and the port. In this case database:3306
, but as I wrote this also didn’t work.
Can someone explain to me, what I did wrong?
Update
I wanted to check if I can access the database from the php container. So I run ping database
inside of the php container and I got pings back. So the connection between the container works, but not from symfony side. And this is what I don’t understand