CakePHP – 如何通过在CakePHP项目中使用套接字连接到docker的db容器

我正试图通过使用docker而不是MAMP来开发我的web应用程序。 这是我第一次做这个工作。 做了很多search之后。 我终于能够链接到我的数据库容器,并看到标准的所有绿色的CakePHP默认页面。

但是当我尝试使用下面的CakePHP的烘焙function。

bin/cake bake all users 

我得到了下面的错误。

 Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in [CakePhpProjects/cakephptest/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php, line 48] 

但是当我使用MAMP的环境并且在config / app.php的“DataResource”的地方添加了下面的代码时,同样的function正在工作。

 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 

那么有什么办法来解决这个问题?

我的DockerFile是这样的。

 FROM ubuntu RUN apt-get update \ && apt-get install -y \ composer \ curl \ php \ php-intl \ php-mbstring \ php-mysql \ unzip \ zip RUN mkdir /code WORKDIR /code ADD . /code/ 

而我的docker-compose.yml文件是这样的

 version: '2' services: db: image: mysql environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=cakephptest - MYSQL_USER=root - MYSQL_PASSWORD=root web: build: . command: cakephptest/bin/cake server -H 0.0.0.0 volumes: - .:/code ports: - "8765:8765" depends_on: - db links: - db phpmyadmin: image: phpmyadmin/phpmyadmin environment: - PMA_ARBITRARY=1 - PMA_HOST=db - PMA_USER=root - PMA_PASSWORD=root links: - db ports: - 8080:80 volumes: - /sessions 

我的CakePHP的数据库设置是这样的

 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', /** * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', 'username' => 'root', 'password' => 'root', 'database' => 'caketest', 'encoding' => 'utf8', 'timezone' => 'UTC', 'flags' => [], 'cacheMetadata' => true, 'log' => false, /** * Set identifier quoting to true if you are using reserved words or * special characters in your table or column names. Enabling this * setting will result in queries built using the Query Builder having * identifiers quoted when creating SQL. It should be noted that this * decreases performance because each query needs to be traversed and * manipulated before being executed. */ 'quoteIdentifiers' => false, /** * During development, if using MySQL < 5.6, uncommenting the * following line could boost the speed at which schema metadata is * fetched from the database. It can also be set directly with the * mysql configuration directive 'innodb_stats_on_metadata = 0' * which is the recommended value in production environments */ //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], 'url' => env('DATABASE_URL', null), 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', ],