I have a problem with docker and mysql (i am pretty new with docker)
I am trying to use symfony 3.4 with docker.
I have this error :
In AbstractMySQLDriver.php line 103:
An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddre
sses: getaddrinfo failed: Name does not resolveIn PDOConnection.php line 47:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name d
oes not resolveIn PDOConnection.php line 43:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name d
oes not resolveIn PDOConnection.php line 43:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name does
not resolve
And i really don’t know what to do …
Here is my docker-compose :
version: "3"
services:
apache:
build: ./docker/apache/
ports:
- "80:80"
depends_on:
- php
php:
build: .
ports:
- "8080:8000"
db:
image: mysql:8.0.0
ports:
- "3306:3306"
volumes:
- /docker/database:/etc/mysql/conf.d
restart: always
environment:
- MYSQL_ROOT_PASSWORD=toor
- MYSQL_USER=me
- MYSQL_PASSWORD=pwd
- MYSQL_DATABASE=name
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=db
- PMA_PORT=3306
ports:
- "80:80"
links:
- db
My docker file :
FROM php:7.3-rc-zts-alpine
RUN curl -sS https://getcomposer.org/installer |
php -- --install-dir=/usr/bin/ --filename=composer
WORKDIR /app
COPY . .
RUN COMPOSER_MEMORY_LIMIT=-1 composer install
RUN docker-php-ext-install pdo_mysql
RUN php bin/console doctrine:schema:update --force
COPY . .
EXPOSE 8080 8000
CMD ["php", "bin/console", "server:run", "0.0.0.0:8000"]
And my config for symfony :
parameters:
database_host: db
database_port: 3306
...
2
Answers
"SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve" suggests that the dns name for the database server failed.
I am not sure what you are using for your database_host, but it looks like you are assuming your symfony config is going to resolve the location of "db" from the docker compose process. This is not the case. You need to create your symfony config database host based on the ip address running the database container. When you add container orchestration to the mix, things will start getting even more complicated. You should look into some form of service discovery to help manage this problem.
When you run docker-compose up , all containers will start , the issue you have is that your php container start too quickly .
And after , the database server will take a long time to start 1 sec or more.
You php is not patient , and does not retry .
You must do a loop to check if you db is up , in my example i use
nc -z db 3306
to see if i connect to the port 3306 of the hostdb
A example of a setup with a Docker-compose.yml
and a docker file