【问题标题】:Composer within Dockerfile on Google Cloud RunGoogle Cloud Run 上 Dockerfile 中的 Composer
【发布时间】:2022-01-30 13:15:01
【问题描述】:

我的应用程序已成功部署到 Google Cloud Run,并且是一个基于 PHP 的网站。该网站使用需要 composer google/apiclient 的 Google 登录 API。

我不知道如何在 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"
    }
}

我找到了this post,并尝试了mentioned here 的多阶段构建方法,方法是在我的Dockerfile 末尾包含以下内容,但这会在构建时引发错误:

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

当我尝试查看日志时,控制台通知我我没有所需的权限。为了查看错误,我按照this post添加了Cloud Build Viewer和Cloud 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 语句放错了位置?

【问题讨论】:

    标签: google-cloud-platform dockerfile composer-php google-cloud-run


    【解决方案1】:

    您无需设置多阶段构建即可运行composer,只需在php:8.0-apacheDockerfile 处安装并运行composer。

    有一些选择。

    在没有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 中的部门一起安装,因为它是WORKDIR。

    【讨论】:

    • 太棒了!这大大简化了我的 Dockerfile 和构建。我想知道是否应该对 /var/www/html 中安装的 composer.phar 做点什么?在正常安装中,composer.phar 会进入 /usr/local/bin/composer?
    【解决方案2】:

    我认为问题在于我试图从自身复制作曲家安装(构建器)。我找到了this link,这表明可以先完成composer安装,然后在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 . 将结果复制到 composer 构建步骤中?

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2021-09-15
      • 2018-10-13
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2022-11-08
      • 2021-01-20
      • 2019-03-26
      相关资源
      最近更新 更多