From 3a3cf576e5c710179b2b2ccfcf1ea63bf9f3295b Mon Sep 17 00:00:00 2001 From: Niko Date: Thu, 10 Feb 2022 01:32:00 +0100 Subject: [PATCH] Added mongodb support for PHP "web" service * Created new Dockerfile with the needed installs * Created database.php simple class for MongoDB simplify and queries --- docker-compose.yaml | 4 +++- web/Dockerfile | 5 +++++ web/src/database.php | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 web/Dockerfile create mode 100644 web/src/database.php diff --git a/docker-compose.yaml b/docker-compose.yaml index d979527..d159f72 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -23,11 +23,13 @@ services: web: command: php -S 0.0.0.0:8080 - image: php:8-alpine + build: web/ volumes: - ./web/src:/src environment: - DOMAIN=${DOMAIN} + - DB_HOST=${DB_HOST} + - DB_NAME=${DB_NAME} working_dir: /src networks: - fedilove diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 0000000..b69f0cc --- /dev/null +++ b/web/Dockerfile @@ -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 diff --git a/web/src/database.php b/web/src/database.php new file mode 100644 index 0000000..a955646 --- /dev/null +++ b/web/src/database.php @@ -0,0 +1,21 @@ +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(); + } +}