Added mongodb support for PHP "web" service

* Created new Dockerfile with the needed installs
* Created database.php simple class for MongoDB simplify and queries
This commit is contained in:
Niko 2022-02-10 01:32:00 +01:00
parent 4cf4de2b20
commit 3a3cf576e5
3 changed files with 29 additions and 1 deletions

View File

@ -23,11 +23,13 @@ services:
web: web:
command: php -S 0.0.0.0:8080 command: php -S 0.0.0.0:8080
image: php:8-alpine build: web/
volumes: volumes:
- ./web/src:/src - ./web/src:/src
environment: environment:
- DOMAIN=${DOMAIN} - DOMAIN=${DOMAIN}
- DB_HOST=${DB_HOST}
- DB_NAME=${DB_NAME}
working_dir: /src working_dir: /src
networks: networks:
- fedilove - fedilove

5
web/Dockerfile Normal file
View File

@ -0,0 +1,5 @@
FROM php:8-alpine
RUN apk add autoconf gcc libc-dev make --update
RUN pecl install mongodb
RUN echo 'extension=mongodb.so' > /usr/local/etc/php/conf.d/docker-php-ext-mongodb.ini

21
web/src/database.php Normal file
View File

@ -0,0 +1,21 @@
<?php
class DB {
private $manager;
private $db_name;
function __construct() {
$db_host = getenv('DB_HOST');
$db_name = getenv('DB_NAME');
if ($db_host === false || $db_name === false)
die('"DB_HOST|DB_NAME" environments are not defined');
$this->manager = new MongoDB\Driver\Manager("mongodb://$db_host:27017");
$this->db_name = $db_name;
}
function query($table, $q) {
$query = new MongoDB\Driver\Query($q);
$cursor = $manager->executeQuery($this->db_name.'.'.$table, $query);
return $cursor->toArray();
}
}