Reputation:
I have the following docker-compose.yml, mostly copied from some tutorial (https://medium.com/@romaricp/the-perfect-kit-starter-for-a-symfony-4-project-with-docker-and-php-7-2-fda447b6bca1):
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
- 443:443
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4:cached
depends_on:
- php
mysql:
image: mysql:5.7
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
restart: always
ports:
- 3306:3306
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: sf4_mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Now my problem is, that I cannot manage to connect to MySQL. Wheter from PHP nor PhpMyAdmin. I've tried the following connection strings, but none of them work:
DATABASE_URL=mysql://root:root@mysql:3306/sf4
DATABASE_URL=mysql://sf4:sf4@mysql:3306/sf4
DATABASE_URL=mysql://root:root@sf4_mysql:3306/sf4
DATABASE_URL=mysql://sf4:sf4@sf4_mysql:3306/sf4
... and hundreds more
But I always get:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Or
SQLSTATE[HY000] [2002] No route to host
Or
SQLSTATE[HY000] [2002] Connection refused
Upvotes: 3
Views: 9428
Reputation:
Thanks to @emix and @LinPy, who helped me out in the comments, here is the fixed configuration:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
- 443:443
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4:cached
depends_on:
- php
mysql:
image: mysql:5.7
container_name: sf4_mysql
#command: "--default-authentication-plugin=mysql_native_password"
#restart: always
ports:
- 3306:3306
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Connection string:
DATABASE_URL=mysql://sf4:sf4@mysql/sf4
Upvotes: 2