Search code examples
phpmysqldockercakephp

Docker cant connect to cake php


HY i have a docker container on the setup i get a error that the system is unable to connect to the database i cant figure it out how this comes.

i tried connecting to localhost, 127.0.0.1 and mysql but i get the same error:

CakePHP is NOT able to connect to the database. Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory

cant seem to find anything in the log files (apache and cake), what am i missing ?

php code:

 'default' => [
            //'className' =>'Cake\Database\Connection',
            'className' => Connection::class,
            'driver' => Mysql::class,
            //'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'timezone' => 'UTC',
            'encoding' => 'utf8',
            'host' => 'mysql',
            'username' => 'myusername',
            'password' => 'mypassword',
            'database' => 'myadatabase',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,
        ],

docker-compose.yml

#File version 2.1
version: "3"


services:
  webserver:
    build: 
      context: ./bin/webserver
    container_name: '7.3.x-webserver'
    restart: 'always'
    ports:
      - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
      - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
    links:
      - mysql
    volumes: 
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
  mysql:
    build:
      context: "./bin/${DATABASE}"
    container_name: '5.7-mysql'
    restart: 'always'
    ports:
      - "${HOST_MACHINE_MYSQL_PORT}:3306"
    volumes: 
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: 'sc-phpmyadmin'
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_USER: ${MYSQL_USER}
      PMA_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    ports:
      - '8080:80'
    volumes: 
      - /sessions
#  redis:
#    container_name: 'sc-redis'
#    image: redis:latest
#    ports:
#      - "${HOST_MACHINE_REDIS_PORT}:6379"

docker build script:

FROM php:7.3-apache-stretch

# suppression of debconfig  complaints of  install apt packages interactively 
ARG DEBIAN_FRONTEND=noninteractive

# Update
RUN apt-get -y update --fix-missing && \
    apt-get upgrade -y && \
    apt-get --no-install-recommends install -y apt-utils && \
    rm -rf /var/lib/apt/lists/*


# Install useful tools and install important libaries
RUN apt-get -y update && \
    apt-get -y --no-install-recommends install nano wget dialog libsqlite3-dev libsqlite3-0 && \
    apt-get -y --no-install-recommends install mysql-client zlib1g-dev libzip-dev libicu-dev && \
    apt-get -y --no-install-recommends install --fix-missing apt-utils build-essential git curl && \ 
    apt-get -y --no-install-recommends install --fix-missing libcurl3 libcurl3-dev zip openssl && \
    rm -rf /var/lib/apt/lists/* && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install xdebug
RUN pecl install xdebug-2.7.2 && \
    docker-php-ext-enable xdebug

# Install redis
RUN pecl install redis-5.0.2 && \
    docker-php-ext-enable redis

# Other PHP7 Extensions

RUN docker-php-ext-install pdo_mysql && \
    docker-php-ext-install pdo_sqlite && \
    docker-php-ext-install mysqli && \
    docker-php-ext-install curl && \
    docker-php-ext-install tokenizer && \
    docker-php-ext-install json && \
    docker-php-ext-install zip && \
    docker-php-ext-install -j$(nproc) intl && \
    docker-php-ext-install mbstring && \
    docker-php-ext-install gettext

# Install Freetype 
RUN apt-get -y update && \
    apt-get --no-install-recommends install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
    rm -rf /var/lib/apt/lists/* && \
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
    docker-php-ext-install -j$(nproc) gd

# Enable apache modules
RUN a2enmod rewrite headers

# Cleanup
RUN rm -rf /usr/src/*


Solution

  • hy i changed the app.php with the env variables changed the username and password of the database.

    i removed the data of ./data/mysql rebuild the container but still the same error my gues it hase something to to with the hostname

    tried also with the variable PMA_HOST but still no go. (localhost does not work either)

     'host' => env('PMA_HOST'),
    

    if i login in to phpmyadmin i can create my tables without any problem.

    if i login in to the container i can loging and do also the querys

    i saw that the socket was not filled in by grepping the php settings,

    pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

    now this is filled in but still a no go on php connection to mysql (on cake, in my test script for a database i have connection).