I am trying to create a LEMP in Linux with docker-compose, I have managed to get PHP, NGINX and MySQL to work for me but when connecting to the DB from a php file it returns the exception: could not find driver
This is my docker-compose.yml file
web: build: . nginx: image: tutum/nginx ports: - "80:80" volumes: - ./sites-available/default:/etc/nginx/sites-available/default - ./sites-available/default:/etc/nginx/sites-enabled/default - ./logs/nginx-error.log:/var/log/nginx/error.log - ./logs/nginx-access.log:/var/log/nginx/access.log phpfpm: image: php:fpm ports: - "9000:9000" volumes: - ./public_html:/usr/share/nginx/html mysql: image: mysql:5.7 command: --default-authentication-plugin=mysql_native_password restart: always ports: - "33061:3306" volumes: - ./public_html/Model:/docker-entrypoint-initdb.d environment: MYSQL_DATABASE: tienda MYSQL_USER: user MYSQL_PASSWORD: user MYSQL_ROOT_PASSWORD: root
This is my DockerFile
FROM php:7.2.19-apache-stretch # PHP extensions RUN docker-php-ext-install mysqli pdo pdo_mysql
This is the PHP file that connects to the DB
<?php abstract class TiendaDB { private static $server = 'localhost'; private static $db = 'tienda'; private static $user = 'root'; private static $password = 'root'; public static function connectDB() { try { $connection = new PDO("mysql:host=".self::$server.";dbname=".self::$db.";charset=utf8", self::$user, self::$pass>} catch (PDOException $e) { echo "No se ha podido establecer conexión con el servidor de bases de datos.<br>"; die ("Error: " . $e->getMessage()); } return $connection; } }
Advertisement
Answer
I have managed to solve it, I have added the Dockerfile file that @β.εηοιτ.βε and it gave me another error so I have done it all again and it has worked for me:
Folder Structure:
DOCKER_LEMP ├── app # All of your php file should be placed here, treat it like /var/www directory │ └── index.php # Default index file ├── database # The database is saved here ├── nginx # This folder contains nginx specified configuration │ └── default.conf # Default configuration file ├── php # Contains custom PHP7-fpm docker configuration │ └── Dockerfile # Docker configuration file for PHP container ├── docker-compose.yml # Docker configuration for this project └── readme.md # Description for this project
Nginx config:
server { listen 80; index index.php index.html; server_name localhost; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /app; location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } }
docker-compose.yml config
version: '3' networks: LEMP: services: nginx: image: nginx:stable-alpine container_name: LEMP_nginx ports: - "80:80" volumes: - ./app:/app - ./nginx/default.conf:/etc/nginx/conf.d/default.conf depends_on: - php networks: - LEMP mysql: image: mysql:latest container_name: LEMP_mysql volumes: - ./database:/var/lib/mysql:rw ports: - "3306:3306" depends_on: - nginx environment: - MYSQL_ROOT_PASSWORD=admin123 networks: - LEMP php: # image: php:7-fpm-alpine build: ./php container_name: LEMP_php volumes: - ./app:/app ports: - "9000:9000" networks: - LEMP phpmyadmin: image: phpmyadmin/phpmyadmin container_name: LEMP_phpmyadmin ports: - "8000:80" environment: PMA_ARBITRARY: 1 depends_on: - mysql networks: - LEMP
Dockerfile config:
FROM php:7-fpm-alpine RUN docker-php-ext-install mysqli pdo pdo_mysql