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

I have this error since three days & I tried to know the reason, but I couldn't. I'm beginner in Docker & Symfony. I made docker containers for php, mysql & nginx in my project directory from this Tutorial , then I installed symfony 5.

When I try to execute the following command inside docker container, where is my project:

bin/console doctrine:database:create

the error after the executing of the command

my directory it's like this:

  • symfony_docker

    • app (dir)
      • symfony files are here
    • mysql (dir)
    • nginx (dir)
      • default.conf (file)
    • php (dir)
      • Dockerfile (file)
    • docker-compose.yml (file)

    that is the content of the docker-compose.yml (file)

version: '3.3'

services:
 database:
   container_name: database
   image: mysql:8.0
   restart: always
   command: --default-authentication-plugin=mysql_native_password
   environment:
     MYSQL_ROOT_PASSWORD: secret
     MYSQL_DATABASE: symfony_docker
     MYSQL_USER: symfony
     MYSQL_PASSWORD: symfony
   ports:
     - '4306:3306'
   volumes:
     - ./mysql:/var/lib/mysql
   
   
     php:
       container_name: php
       build:
         context: ./php
       ports:
         - '9000:9000'
       volumes:
         - ./app:/var/www/symfony_docker
   
       depends_on:
         - database
   
     nginx:
       container_name: nginx
       image: nginx:stable-alpine
       ports:
         - '8080:80'
       volumes:
         - ./app:/var/www/symfony_docker
         - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
       depends_on:
         - php
         - database

That is the content of the php Dockerfile

FROM php:8.0-fpm

RUN apt update \
   && apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
   && docker-php-ext-install intl opcache pdo pdo_mysql \
   && pecl install apcu \
   && docker-php-ext-enable apcu \
   && docker-php-ext-configure zip \
   && docker-php-ext-install zip

WORKDIR /var/www/symfony_docker

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony
RUN git config --global user.email "[email protected]" \
   && git config --global user.name "test"

the content of the default.conf

server {

    listen 80;
    index index.php;
    server_name localhost;
    root /var/www/symfony_docker/public;
    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\\.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;

        internal;
    }

    location ~ \\.php$ {
        return 404;
    }

}

content of .env in symfony

# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=ace4f16ab212b0206302e83d3939664f
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
# DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"

DATABASE_URL="mysql://root:secret@mysql:4306/menukarte?serverVersion=5.7"

###< doctrine/doctrine-bundle ###

I tried already to change the DATABASE_URL= to

  1. @mysql
  2. @localhost
  3. @27.0.0.1 no one of them resolved the issue

thank you very much



Solution 1:[1]

I added the host as you said, and I changed the port to 3306 Like this: DATABASE_URL="mysql://root:secret@database:3306/menukarte?serverVersion=5.7" –

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dr.IT79