【问题标题】:Docker php mongodb installation error using composerDocker php mongodb使用composer安装错误
【发布时间】:2020-11-25 07:21:54
【问题描述】:

我在我的 Laravel 项目中使用 mongodb 和 php。当我尝试运行此命令时:

docker-compose exec php composer install

然后在控制台中收到错误消息:

Failed to download mongodb/mongodb from dist: The zip extension and unzip command are both missing, skipping. Your command-line PHP is using multiple ini files. Run `php --ini` to show them.

Now trying to download from source

- Installing mongodb/mongodb (1.6.1): Cloning 4bb040c620

Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+95edf1f219f1+2020-08-05+0622
to retrieve a token. It will be stored in "/root/.composer/auth.json" for future use by Composer.
Token (hidden):

这是我所有的文件:

文件:docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "${HOST_PORT}:80"
    volumes:
      - ../:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
  php:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: php
    volumes:
      - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini
      - ../:/var/www/html
  mongodb:
    image: mongo:latest
    container_name: mongodb
    ports:
      - "${MONGODB_PORT}:27017"
  redis:
    image: redis:latest
    container_name: redis
    restart: always
    ports:
      - "${REDIS_PORT}:6379"
  artisan:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ../:/var/www/html
    working_dir: /var/www/html
    entrypoint: ['/var/www/html/artisan']

文件 Dockerfile

FROM php:7.4-fpm

RUN apt-get update && apt-get install --yes --no-install-recommends \
    libssl-dev

RUN docker-php-ext-install pdo pdo_mysql

RUN pecl install mongodb \
    && docker-php-ext-enable mongodb

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

用于 docker 的文件 .env

HOST_PORT=8004
HOST_SSL_PORT=3004

# Nginx
NGINX_HOST=localhost

# See https://hub.docker.com/r/nanoninja/php-fpm/tags/
PHP_VERSION=latest

MONGODB_PORT=27017
REDIS_PORT=6379

文件php.ini

[Xdebug]
xdebug.remote_enable=1
xdebug.idekey=PHPSTORM
xdebug.profiler_enable=0
xdebug.max_nesting_level=700
xdebug.remote_host=192.168.0.1 # your ip
xdebug.remote_port=9000

文件 nginx default.conf

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

【问题讨论】:

  • 根据您的 Linux 风格和 PHP 版本,这些可能会有所不同。 (sudo) yum install zip unzip php-zip(sudo) apt install zip unzip php-zip
  • 你得到的错误是我在构建 PHP 映像期间所期望的,但是你在exec 期间得到了这个?如果你显式运行 docker-compose build 会发生什么?
  • 另外,如果您需要 PHP 扩展,这总是对我有很大帮助:github.com/mlocati/docker-php-extension-installer
  • 我在使用这个 docker 命令构建后运行 composer 命令:docker-compose up --build -d@onlineThomas
  • @STA 我尝试使用命令RUN docker-php-ext-install pdo pdo_mysql zip unzip php-zip 安装扩展,但得到错误error: /usr/src/php/ext/unzip does not exist

标签: php laravel mongodb docker composer-php


【解决方案1】:

更改你的 dockerfile

    FROM php:7.4-fpm
    
    ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
    
    RUN install-php-extensions \
    pdo_mysql \
    zip \
    mongodb
    
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

【讨论】:

  • 请注意我更新了install-php-extensions脚本的获取方式:使用https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions而不是https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions
猜你喜欢
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 2018-09-15
  • 2013-07-20
  • 2014-12-07
  • 2021-05-02
  • 2015-11-01
相关资源
最近更新 更多