KrzysiekW
KrzysiekW

Reputation: 41

Deployment failed to start and listen on the port with nginx + php

I have a problem with deploying to google cloud. I have a dockerfile which nginx and php8.1. Locally, everything works as it should, but when I deploy on google cloud I get an error: The user-provided container failed to start and listen on the port defined provided by the PORT = 8080 environment variable. Service in google cloud is configured by default on port 8080. I noticed that nginx without php builds normally and starts but with php doesn't work and gets this error like above.

dockerfile

FROM nginx:alpine as nginx

COPY nginx/default.conf /etc/nginx/conf.d/default.conf

FROM php:8.1-fpm AS php

COPY docker/php/php.ini /usr/local/etc/php/conf.d/docker-php-config.ini

RUN apt-get update; \
    apt-get upgrade -y; \
    apt-get install -y --no-install-recommends \
            curl \
            libmemcached-dev \
            libz-dev \
            libpq-dev \
            libjpeg-dev \
            libpng-dev \
            libfreetype6-dev \
            libssl-dev \
            libwebp-dev \
            libxpm-dev \
            libmcrypt-dev \
            libonig-dev; \
    rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install pdo

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

WORKDIR /var/www/symfony

I added an EXPOSE 8080 but it doesn't work with that either

nginx conf

server {
    listen 8080;
    server_name localhost;
    root /var/www/symfony/public;

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

    location ~ ^/index\.php(/|$) {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
            add_header 'Access-Control-Max-Age' 1728000 always;
            add_header 'Content-Type' 'text/plain; charset=utf-8' always;
            add_header 'Content-Length' 0 always;
            return 204;
        }
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

        fastcgi_pass php: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/stdout info;
    access_log /var/log/nginx/project_access.log;
}

I have read various posts on the stack overflow e.g. Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable but it doesn't solve my problem. Any suggestions what I have wrong ?

In cloud logging i have this:

{
  "textPayload": "Step #2: ERROR: (gcloud.beta.run.deploy) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.",
  "insertId": "8f70f71c-fc36-4b7d-b903-25e9c770342f-2124",
  "resource": {
    "type": "build",
    "labels": {
      "build_id": "8f70f71c-fc36-4b7d-b903-25e9c770342f",
      "build_trigger_id": "",
      "project_id": "adroit-sol-340313"
    }
  },
  "timestamp": "2022-10-19T11:01:20.973405245Z",
  "severity": "INFO",
  "labels": {
    "build_step": "Step #2"
  },
  "logName": "projects/adroit-sol-340313/logs/cloudbuild",
  "receiveTimestamp": "2022-10-19T11:01:21.466513119Z"
}

Upvotes: 0

Views: 362

Answers (1)

Sarah Remo
Sarah Remo

Reputation: 709

As per @KrzysiekW, I tested on beta and forgot to change but previously tested without beta. Finally I changed my docker build and is currently building from richarvey/nginx-php-fpm image. Everything works now. Thanks for the help.

Upvotes: 1

Related Questions