Google Cloud 运行 上的 Dockerfile 中的 Composer

Composer within Dockerfile on Google Cloud Run

我的应用程序已成功部署到 Google 云 运行,并且是一个基于 PHP 的网站。该站点使用 Google 的登录 API,这需要作曲家 google/apiclient.

我不知道如何在 Dockerfile 中构建所需的 /vendor 文件夹结构(即安装 composer)。我目前的解决方法是在我的代码中包含 /vendor 文件夹结构并将其构建为 cloud run deploy ....

的一部分

我的 Dockerfile(其中不包含对 composer 和 google/apiclient 要求的引用)如下所示:

# Use the official PHP image.
# https://hub.docker.com/_/php
FROM php:8.0-apache

# Configure PHP for Cloud Run.
# Precompile PHP code with opcache.
RUN docker-php-ext-install -j "$(nproc)" opcache
RUN docker-php-ext-install -j "$(nproc)" mysqli
RUN set -ex; \
  { \
    echo "; Cloud Run enforces memory & timeouts"; \
    echo "memory_limit = -1"; \
    echo "max_execution_time = 0"; \
    echo "; File upload at Cloud Run network limit"; \
    echo "upload_max_filesize = 32M"; \
    echo "post_max_size = 32M"; \
    echo "; Configure Opcache for Containers"; \
    echo "opcache.enable = On"; \
    echo "opcache.validate_timestamps = Off"; \
    echo "; Configure Opcache Memory (Application-specific)"; \
    echo "opcache.memory_consumption = 32"; \
  } > "$PHP_INI_DIR/conf.d/cloud-run.ini"

# Copy in custom code from the host machine.
WORKDIR /var/www/html
COPY . ./

# Use the PORT environment variable in Apache configuration files.
# https://cloud.google.com/run/docs/reference/container-contract#port
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf

# Configure PHP for development.
# Switch to the production php.ini for production operations.
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# https://github.com/docker-library/docs/blob/master/php/README.md#configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

我的 composer.json 文件:

{
    "require": {
        "google/apiclient": "^2.11"
    }
}

我通过在我的 Dockerfile 末尾包含以下内容找到 , and attempted the multi-stage build approach as mentioned here,但这会在构建时引发错误:

FROM composer as builder
WORKDIR /app/
COPY composer.* ./
RUN composer install
COPY --from=builder /app/vendor /var/www/html/vendor

当我尝试查看日志时,控制台通知我没有所需的权限。我根据 添加了 Cloud Build ViewerCloud Build Editor 以查看错误。

前面的所有步骤都已完成,我可以看到错误状态:

Step 13/13 : COPY --from=builder /app/vendor /var/www/html/vendor
invalid from flag value builder: pull access denied for builder, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1

如何使用成功的 composer 构建引用容器?我是否需要其他步骤,或者 COPY 语句是否在错误的位置?

我认为问题在于我试图从自身复制作曲家安装(生成器)。我发现 this link 这表明可以先完成作曲家安装,然后在 php 构建中引用(它也复制了我的源文件)。

我更新后的 Dockerfile 如下所示,构建成功!

#Install composer
FROM composer:latest as composer
WORKDIR /var/www/html
#COPY the composer.json and composer.lock files to the working directory
COPY composer.* ./
RUN composer install

# Use the official PHP image.
# https://hub.docker.com/_/php
FROM php:8.0-apache
#Copy the composer install (/vendor folders) into the working directory
COPY --from=composer /var/www/html .

# Use the official PHP image.
# https://hub.docker.com/_/php
FROM php:8.0-apache

# Configure PHP for Cloud Run.
# Precompile PHP code with opcache.
RUN docker-php-ext-install -j "$(nproc)" opcache
RUN docker-php-ext-install -j "$(nproc)" mysqli
RUN set -ex; \
  { \
    echo "; Cloud Run enforces memory & timeouts"; \
    echo "memory_limit = -1"; \
    echo "max_execution_time = 0"; \
    echo "; File upload at Cloud Run network limit"; \
    echo "upload_max_filesize = 32M"; \
    echo "post_max_size = 32M"; \
    echo "; Configure Opcache for Containers"; \
    echo "opcache.enable = On"; \
    echo "opcache.validate_timestamps = Off"; \
    echo "; Configure Opcache Memory (Application-specific)"; \
    echo "opcache.memory_consumption = 32"; \
  } > "$PHP_INI_DIR/conf.d/cloud-run.ini"

# Copy in custom code from the host machine.
WORKDIR /var/www/html
COPY . ./

# Use the PORT environment variable in Apache configuration files.
# https://cloud.google.com/run/docs/reference/container-contract#port
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf

# Configure PHP for development.
# Switch to the production php.ini for production operations.
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# https://github.com/docker-library/docs/blob/master/php/README.md#configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

大概我同样可以将引用的 PHP 构建步骤称为 FROM php:8.0-apache as PHPBuild,然后使用 COPY --FROM PHPBuild /var/www/html .?

将结果复制到作曲家构建步骤中

您不需要将 multi-stage 构建到 运行 composer,您只需安装并 运行 composerphp:8.0-apache Dockerfile.

有一些选项。

安装没有 composer.json 文件的作曲家:

RUN apt update && apt install -y zip
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && chmod +x /usr/local/bin/composer
RUN composer require google/apiclient:^2.11 \
    && composer clear-cache

正在使用 composer.json 文件安装作曲家:

RUN apt update && apt install -y zip
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && chmod +x /usr/local/bin/composer
RUN composer install \
    && composer clear-cache

composer.json:

{
    "require": {
        "google/apiclient": "^2.11"
    }
}

这样,composer 将与 /var/www/html 中的 deps 一起安装,因为它是 WORKDIR