CakePHP - 如何使用CakePHP项目

时间:2017-04-14 03:07:13

标签: php cakephp docker

我尝试使用docker而不是MAMP来开发我的Web应用程序。这是我第一次做这个工作。经过大量的搜索。我终于能够链接到我的数据库容器,并看到所有绿色的标准CakePHP默认页面。

但是当我尝试使用CakePHP的烘焙功能时。

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的环境并在" DataResource"的位置添加以下代码时,同样的功能正在工作。 config / app.php。

'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',
        ],

1 个答案:

答案 0 :(得分:0)

您应该使用docker容器的主机名而不是localhost:

'host' => 'db'