brn
brn

Reputation: 305

Database connection issue through docker containers

I have set up a dev environment with Symfony and MariaDB through docker.

I've taken the bitman/symfony image there :

 https://hub.docker.com/r/bitnami/symfony

This is my docker-compose.yaml

version: '2'

services:
  myapp:
    image: 'bitnami/symfony:1'
    ports:
      - '8000:8000'
    volumes:
      - '.:/app'
    environment:
      - SYMFONY_PROJECT_NAME=backend
      - MARIADB_PORT_NUMBER=3306
      - MARIADB_USER=root
    container_name: symfony_container
    depends_on:
      - mariadb
  mariadb:
    image: 'bitnami/mariadb:10.3'
    ports:
      - '3306:3306'
  environment:
    - ALLOW_EMPTY_PASSWORD=yes
    - MARIADB_USER=root
  container_name: mariadb_container

Important line in .env file :

DATABASE_URL=mysql://root:@localhost:3306/

My doctrine.yaml file:

doctrine:
  dbal:
      url: '%env(resolve:DATABASE_URL)%'
      server_version: '5.7'
  orm:
      auto_generate_proxy_classes: true
      naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
      auto_mapping: true
      mappings:
          App:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity'
              prefix: 'App\Entity'
              alias: App

Now if I run docker-compose up, both containers start and I can access Symfony.

BUT if I try to create an entity with :

php bin/console make:entity

I get the four following errors :

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution 

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution  

PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution 

The strange thing is that I can access MariaDB database through dbeaver without trouble. It seems as if the Symfony container did not manage to find the MariaDB container.

I'll precise that I use symfony version 5.0.4

If anyone knows where those errors come from I'll be grateful

Upvotes: 1

Views: 1421

Answers (2)

bcag2
bcag2

Reputation: 2439

I agree with @Zeitounator solution, but in my host, if I launch
php bin/console doctrine:migration:migrate
it returns an error :
could not translate host name "mariadb" to address: Name or service not known I solved by add in my /etc/hosts :
127.0.0.1 mariadb
and it works on host and in container

Upvotes: 0

Zeitounator
Zeitounator

Reputation: 44605

DATABASE_URL=mysql://root:@localhost:3306/

In the context of docker, localhost is the current running container. Since this is used from your symfony container, it will try to find the db there.

From your docker-compose.yml file, your db service is named mariadb and will be resolved as such from the containers participating in your application default network:

DATABASE_URL=mysql://root:@mariadb:3306/

Note: I suspect you may have other problems to solve related to your db users settings once the connection is fixed. If this is the case, carefully review the your db image configuration documentation.

Upvotes: 3

Related Questions