【发布时间】: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