alextselegidis / easyappointments

:date: Easy!Appointments - Self Hosted Appointment Scheduler
https://easyappointments.org
GNU General Public License v3.0
3.28k stars 1.26k forks source link

No such file or directory in /var/www/html/MongoDBTest.php #1600

Open alberto-fraccarollo opened 1 day ago

alberto-fraccarollo commented 1 day ago

Hi, my docker file and webapp are so configurated. I recieve the error "Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/MongoDBTest.php on line 3. Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html/MongoDBTest.php on line 3".

How to fix it? I watch in my docker the directory vendor in PATH /var/www/html/vendor, but the directory is empty.

Dockerfile

Use the official PHP 7.4 with Apache image

FROM php:7.4-apache

Set the working directory

WORKDIR /var/www/html

USER root

Install necessary PHP extensions

RUN apt-get update && apt-get install -y \ libcurl4-openssl-dev \ pkg-config \ libssl-dev \ zlib1g-dev \ libzip-dev \ unzip \ git \ && pecl install mongodb \ && pecl install redis \ && docker-php-ext-enable mongodb redis \ && docker-php-ext-install zip mysqli

Copy custom PHP settings

COPY php.ini /usr/local/etc/php/conf.d/

Copy Composer files and install dependencies

COPY composer.json ./ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \ composer install --no-dev --optimize-autoloader

Copy the application source

COPY src/ ./

Expose port 80

EXPOSE 80

docker-composer

services: php-app: build: context: . # Set context to project-root dockerfile: Dockerfile # Path to the Dockerfile volumes:

networks: backend: {}

volumes: documentdb_data: {} redis_data: {}

composer.json { "name": "webapp-ecs/php-application", "description": "A simple PHP application using Docker, MongoDB, and Redis.", "require": { "php": "^7.4", "ext-mysqli": "", "ext-mongodb": "", "ext-redis": "*", "slim/slim": "^4.0", "monolog/monolog": "^2.0", "mongodb/mongodb": "^1.9" }, "autoload": { "psr-4": { "YourNamespace\": "src/" } } }

MongoDBTest.php in /src

<?php

require 'vendor/autoload.php'; // Include the Composer autoload file

use MongoDB\Client;

try { // MongoDB connection details $username = getenv('MONGO_INITDB_ROOT_USERNAME'); $password = getenv('MONGO_INITDB_ROOT_PASSWORD'); $databaseName = getenv('MONGO_INITDB_DATABASE');

// MongoDB connection string
$mongoDbUri = "mongodb://$username:$password@documentdb:27017/$databaseName"; // Use the service name "documentdb"

// Create MongoDB Client
$client = new Client($mongoDbUri);

// Select the database and collection
$database = $client->selectDatabase($databaseName);
$collection = $database->selectCollection('testCollection');

// Insert a document
$insertedId = $collection->insertOne(['name' => 'John Doe', 'email' => 'john.doe@example.com']);
echo "Inserted document with ID: " . $insertedId->getInsertedId() . "<br>";

// Fetch the document
$document = $collection->findOne(['_id' => $insertedId->getInsertedId()]);
echo "Fetched document:<br>";
echo json_encode($document, JSON_PRETTY_PRINT);

} catch (Exception $e) { echo 'Error: ' . $e->getMessage(); }

?>

Regards

Al