简体   繁体   中英

CakePHP - How to connect to docker's db container by using socket in CakePHP project

I'm trying to use to develop my web application by using docker instead of MAMP. And this is my first time to do the job. After doing a lot of search. I finally be able to link to my db container and see standard CakePHP default page with all green.

But when I tried to use CakePHP's bake function below.

bin/cake bake all users

I got the error below.

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]

But the same function is working when I used the environment of MAMP and added the following code in the place of "DataResource" of config/app.php.

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

So is any way to solve this problem?

My DockerFile is this.

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/

And my docker-compose.yml file is this

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

My CakePHP's DB setting is this

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

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

'host' => 'db'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM