0
votes

I developed a project in Windows. I've used PHP, MySQL (using PDO) and Apache.

I created the database and tables with code. There was no problem.

I am trying to dockerize this project on my Ubuntu 18.04 machine. Believe me, I've only been trying this since morning.

The project runs smoothly with CSS, JavaScript on localhost. But it gives error when adding to database. The error code I received was: could not find driver. php.ini file is missing

I also don't have my apache2 folder that should be in etc/php/7.2.

I get the SQLSTATE[HY000] [2002] Cannot assign requested address error I made the connection this way:

$db = new PDO("mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I get the could not find driver error I made the connection this way (using pdo_mysql):

$db = new PDO("pdo_mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I have installed apache2 and php in Dockerfile.

Here is my result of tree command:

.
├── docker-compose.yml
├── Dockerfile
└── src
    ├── css
    │   ├── css
    │   │   └── dashboard.css
    │   ├── fonts
    │   │   └── feather
    │   │       ├── feather-webfont6cfa.eot
    │   │       ├── feather-webfont6cfa.svg
    │   │       ├── feather-webfont6cfa.ttf
    │   │       └── feather-webfont6cfa.woff
    │   └── style.css
    ├── docker-compose.yml
    ├── js
    │   ├── guncelle.js
    │   └── resimler.js
    └── php
        ├── about.php
        ├── categories.php
        ├── create_database.php
        ├── footer.php
        ├── header.php
        ├── index.php
        ├── info.php
        ├── login.php
        └── logout.php

There are docker-compose.yml and Dockerfile files:

# DOCKERFILE
FROM php:7.3-apache
RUN apt-get update
RUN apt-get install -y git
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite
#Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=. --filename=composer
RUN mv composer /usr/local/bin/
COPY src/ /var/www/html/
EXPOSE 80
# DOCKER-COMPOSE.YML
version: '3'
services:
  mysql:
        image: mysql:8.0
        container_name: vt_album
        command: --default-authentication-plugin=mysql_native_password
        volumes:
          - .:/application
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=2222
          - MYSQL_PASSWORD=2222
        ports:
          - "3306:3306"
  website:
    container_name: php_album
    build:
      context: ./
    volumes:
      - ./src/:/var/www/html
    ports:
      - 8080:8080
    depends_on:
      - mysql
2

2 Answers

0
votes

It seems "This image ships with the default php.ini-development and php.ini-production configuration files." and you should choose one

# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
0
votes

I found the solution. The dockerfile and docker-compose.yml files are the same as in the question. No changes. But when connecting to the database, "mysql: host = mysql" should be written instead of "mysql: host = localhost". The second "mysql" here represents the service name we defined in the docker-compose.yml file.

The following code first creates the database. Then, it creates the connection object. Then it calls related methods to create the tables.

    protected $conn;
    protected $username = "root";
    protected $password = 2222;
    protected $dbname = "db_2";

    function _construct(){
        $db = new PDO("mysql:host=mysql;charset=utf8; port:3306", $this->username, $this->password);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $database = "CREATE DATABASE IF NOT EXISTS ".$this->dbname.";ALTER DATABASE ".$this->dbname." CHARACTER SET utf8 COLLATE utf8_turkish_ci;";

        $db->exec($database);

        $this->conn=new PDO("mysql:host=mysql;charset=utf8;port:3306", $this->username, $this->password);
        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $this->create_table_categories();
        $this->create_table_users();
        $this->create_table_files();

        return "<br>success<br>";
    }