在 Laravel Sail 8.0 环境中安装 Jenssegers/MongoDB

Installing Jenssegers/MongoDB in a Laravel Sail 8.0 environment

所以我已经成功地使用 WSL2 在 Windows 中完成了 Laravel 的全新安装。我项目的需求之一是同时有两个数据库运行 mysql 和 mongodb.

我在 docker-compose.yml 中添加了一个 MongoDB 的图像,正如 dock-hub 告诉我的那样,但是当我尝试添加 jenssegers/laravel-mongodb 包 sail 时告诉我我没有正确的要求。

如何正确连接两个容器,以便我可以毫无问题地安装软件包?

日志:

~/example-app$ sail composer require jenssegers/mongodb:"^3.8"
./composer.json has been updated
Running composer update jenssegers/mongodb
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

Problem 1
    - jenssegers/mongodb[v3.8.0, ..., v3.8.1] require mongodb/mongodb ^1.6 -> satisfiable by mongodb/mongodb[1.6.0, ..., 1.9.x-dev (alias of dev-master)].
    - mongodb/mongodb 1.9.x-dev is an alias of mongodb/mongodb dev-master and thus requires it to be installed too.
    - mongodb/mongodb[dev-master, 1.8.0-RC1, ..., v1.8.x-dev] require ext-mongodb ^1.8.1 -> it is missing from your system. Install or enable PHP's mongodb extension.
    - mongodb/mongodb[1.6.0, ..., v1.6.x-dev] require php ^5.6 || ^7.0 -> your php version (8.0.0) does not satisfy that requirement.
    - mongodb/mongodb[1.7.0-beta1, ..., v1.7.x-dev] require php ^7.0 -> your php version (8.0.0) does not satisfy that requirement.
    - Root composer.json requires jenssegers/mongodb ^3.8 -> satisfiable by jenssegers/mongodb[v3.8.0, v3.8.1].

To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php/8.0/cli/php.ini
    - /etc/php/8.0/cli/conf.d/10-mysqlnd.ini
    - /etc/php/8.0/cli/conf.d/10-opcache.ini
    - /etc/php/8.0/cli/conf.d/10-pdo.ini
    - /etc/php/8.0/cli/conf.d/15-xml.ini
    - /etc/php/8.0/cli/conf.d/20-bcmath.ini
    - /etc/php/8.0/cli/conf.d/20-calendar.ini
    - /etc/php/8.0/cli/conf.d/20-ctype.ini
    - /etc/php/8.0/cli/conf.d/20-curl.ini
    - /etc/php/8.0/cli/conf.d/20-dom.ini
    - /etc/php/8.0/cli/conf.d/20-exif.ini
    - /etc/php/8.0/cli/conf.d/20-ffi.ini
    - /etc/php/8.0/cli/conf.d/20-fileinfo.ini
    - /etc/php/8.0/cli/conf.d/20-ftp.ini
    - /etc/php/8.0/cli/conf.d/20-gd.ini
    - /etc/php/8.0/cli/conf.d/20-gettext.ini
    - /etc/php/8.0/cli/conf.d/20-iconv.ini
    - /etc/php/8.0/cli/conf.d/20-igbinary.ini
    - /etc/php/8.0/cli/conf.d/20-imap.ini
    - /etc/php/8.0/cli/conf.d/20-intl.ini
    - /etc/php/8.0/cli/conf.d/20-ldap.ini
    - /etc/php/8.0/cli/conf.d/20-mbstring.ini
    - /etc/php/8.0/cli/conf.d/20-msgpack.ini
    - /etc/php/8.0/cli/conf.d/20-mysqli.ini
    - /etc/php/8.0/cli/conf.d/20-pdo_mysql.ini
    - /etc/php/8.0/cli/conf.d/20-pdo_pgsql.ini
    - /etc/php/8.0/cli/conf.d/20-pdo_sqlite.ini
    - /etc/php/8.0/cli/conf.d/20-pgsql.ini
    - /etc/php/8.0/cli/conf.d/20-phar.ini
    - /etc/php/8.0/cli/conf.d/20-posix.ini
    - /etc/php/8.0/cli/conf.d/20-readline.ini
    - /etc/php/8.0/cli/conf.d/20-redis.ini
    - /etc/php/8.0/cli/conf.d/20-shmop.ini
    - /etc/php/8.0/cli/conf.d/20-simplexml.ini
    - /etc/php/8.0/cli/conf.d/20-soap.ini
    - /etc/php/8.0/cli/conf.d/20-sockets.ini
    - /etc/php/8.0/cli/conf.d/20-sqlite3.ini
    - /etc/php/8.0/cli/conf.d/20-sysvmsg.ini
    - /etc/php/8.0/cli/conf.d/20-sysvsem.ini
    - /etc/php/8.0/cli/conf.d/20-sysvshm.ini
    - /etc/php/8.0/cli/conf.d/20-tokenizer.ini
    - /etc/php/8.0/cli/conf.d/20-xmlreader.ini
    - /etc/php/8.0/cli/conf.d/20-xmlwriter.ini
    - /etc/php/8.0/cli/conf.d/20-xsl.ini
    - /etc/php/8.0/cli/conf.d/20-zip.ini
    - /etc/php/8.0/cli/conf.d/25-memcached.ini
    - /etc/php/8.0/cli/conf.d/99-sail.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

我的docker-compose.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - mongo
            # - selenium

    # ...

    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail

    mongo:
        image: mongo
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: example

    mongo-express:
        image: mongo-express
        restart: always
        ports:
        - 8081:8081
        environment:
            ME_CONFIG_MONGODB_ADMINUSERNAME: root
            ME_CONFIG_MONGODB_ADMINPASSWORD: example    
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local

我设法安装了这个包 :3 首先,我通过 运行 这个命令

提取 Docker 配置
php artisan sail:publish

然后编辑文件 docker/8.0/Dockerfile,在 RUN apt- 之后添加以下行

RUN pecl install mongodb

RUN echo "extension=mongodb.so" > /etc/php/8.0/cli/php.ini

保存,然后运行这条命令重建docker

sail build --no-cache
#Step 1 
#edit vendor/laravel/sail/runtimes/[PHP VERSION]/Dockerfile
#Ex: ./vendor/laravel/sail/runtimes/8.1/Dockerfile

#Step 2 
#Add the command at Dockerfile file
RUN apt-get install -y php8.1-mongodb
  
#Step 3 
#Save it and then run ./vendor/bin/sail build and ./vendor/bin/sail up -d
./vendor/bin/sail build && ./vendor/bin/sail up -d