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.
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:
backend
documentdb: image: mongo:4.2 environment: MONGO_INITDB_DATABASE: webapp_mongo_db MONGO_INITDB_ROOT_USERNAME: mongo_db_master MONGO_INITDB_ROOT_PASSWORD: WXXXXXXXX networks:
documentdb_data:/data/db
redis: image: redis:6.0 networks:
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');
} catch (Exception $e) { echo 'Error: ' . $e->getMessage(); }
?>
Regards
Al