You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Cloud部署Sylius容器失败:无法监听PORT=8080端口求助

解决Cloud Run部署Sylius-Standard的端口监听问题

问题根源

你使用的Sylius 1.12官方Dockerfile默认启动PHP-FPM服务(监听9000端口,非HTTP端口),而Cloud Run要求容器必须提供可直接访问的HTTP服务,并监听平台自动注入的PORT环境变量(默认8080)。同时官方的docker-compose.prod.yml采用Nginx+PHP-FPM多容器架构,但Cloud Run仅支持单容器部署,直接用现有配置无法满足要求。

解决步骤

1. 修改Dockerfile,整合Nginx与PHP-FPM

需要将Nginx打包进容器,通过Supervisor同时启动Nginx和PHP-FPM,并配置Nginx监听PORT变量指定的端口。替换原Dockerfile为以下内容:

FROM php:8.1-fpm-alpine

# 安装Nginx和Supervisor
RUN apk add --no-cache nginx supervisor

WORKDIR /srv/sylius
COPY . .

# 保留原Dockerfile的依赖安装步骤
RUN set -eux; \
    apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        libpng-dev \
        libjpeg-turbo-dev \
        libwebp-dev \
        zlib-dev \
        libzip-dev \
        postgresql-dev \
    ; \
    docker-php-ext-configure gd --with-jpeg --with-webp; \
    docker-php-ext-install -j$(nproc) \
        gd \
        intl \
        opcache \
        pdo_pgsql \
        zip \
    ; \
    pecl install apcu; \
    docker-php-ext-enable apcu; \
    pecl clear-cache; \
    apk del .build-deps

# 配置Nginx监听PORT变量
RUN mkdir -p /run/nginx
COPY docker/nginx/conf.d/default.conf /etc/nginx/conf.d/
RUN sed -i "s/listen 80;/listen \${PORT:-8080};/" /etc/nginx/conf.d/default.conf

# 添加Supervisor配置
COPY docker/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

EXPOSE 8080

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

2. 创建Supervisor配置文件

在项目根目录新建docker/supervisor/supervisord.conf,内容如下:

[supervisord]
nodaemon=true

[program:php-fpm]
command=php-fpm
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=nginx -g "daemon off;"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

3. 调整Nginx配置

确保docker/nginx/conf.d/default.conf中的反向代理指向PHP-FPM的9000端口,配置示例:

server {
    listen ${PORT:-8080};
    server_name localhost;
    root /srv/sylius/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log /dev/stderr notice;
    access_log /dev/stdout;
}

4. 重新部署

执行部署命令,Cloud Run会自动注入PORT变量,容器内的Nginx会自动监听该端口:

gcloud run deploy ds-creative --source .

关键说明

  • 防火墙规则无效:Cloud Run的流量由Google负载均衡器管理,无需配置VPC防火墙,只需确保容器内部监听正确端口。
  • 查看故障日志:若仍失败,执行gcloud run services logs read ds-creative --region <你的区域> --tail查看容器日志,定位具体错误。

内容的提问来源于stack exchange,提问作者ivantxo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:25:29