我已经在Docker中运行了我的开发环境(nginx、php和mariadb),并尝试使用Doctrine在Symfony中创建一个数据库。当我运行php bin/console doctrine:database:create
时,我会得到以下错误:
[critical] Error thrown while running command "doctrine:database:create". Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution"
我在.env
文件中的配置如下:
DATABASE_URL="mysql://admin:symfony-admin@db/symfony_test?serverVersion=mariadb-10.1"
以下是我的docker-compose.yml
文件:
version: "3.6"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- php
php:
build: .
volumes:
- ./code:/code
links:
- db
db:
image: mariadb:latest
restart: always
ports:
- "33006:3306"
volumes:
- ./db:/docker-entrypoint-initdb.d/
environment:
MYSQL_ROOT_PASSWORD: 'symfony-root-pwd'
MYSQL_DATABASE: 'symfony_db'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'symfony-admin'
我也使用Adminer访问数据库,登录可以正常工作。
有人知道为什么用Doctrine无法创建数据库吗?
谢谢,
Michael
解决方案:
我自己找到了解决方案。命令php bin/console doctrine:database:create
需要在php docker容器中运行,而不是在本地终端中运行。所以首先要执行docker-compose exec php /bin/bash
,然后再执行php bin/console doctrine:database:create
。