简体   繁体   English

在 Google Cloud Run 使用 Dockerfile 时,ReactJS process.env.NODE_ENV 未设置为生产环境

[英]ReactJS process.env.NODE_ENV is not set to production when using Dockerfile at Google Cloud Run

I have a ReactJS website that I am running in Google Cloud Run using Dockerfile.我有一个 ReactJS 网站,我使用 Dockerfile 在 Google Cloud Run 中运行。

The website in Google Cloud Run still shows the variable NODE_ENV to development, even after I have specified it to be production in my Dockerfile. Google Cloud Run 中的网站仍然显示变量 NODE_ENV 用于开发,即使我已在我的 Dockerfile 中将其指定为生产环境。

Dockerfile Dockerfile

FROM node:alpine 

# Set the working directory to /app inside the container
WORKDIR /app

# Copy app files
COPY . .

# Install dependencies (npm ci makes sure the exact versions in the lockfile gets installed)
RUN npm ci 

# Build the app
RUN npm run build

# Set the env to "production"
ENV NODE_ENV production

# Expose the port on which the app will be running (3000 is the default that `serve` uses)
EXPOSE 3000

# Start the app
RUN npm i
CMD ["npm", "run", "start"]

Login.js登录.js

import React, { useState, useEffect, useRef } from 'react';

export default function Login() {
  // Backend URL
  let backendURL = ""
  if(process.env.NODE_ENV == "development"){
    backendURL = "https://localhost:5000"
  }
  else{
    backendURL = "https://my-website.a.run.app"
  }

  return(
    <div>
      <p>Please Log in {process.env.NODE_ENV}</p>
    </div>
  )
}

This gives这给

Please Log in development

I have also tried to assign the variable as Enviroment variable by clicking "Edit and deploy new Revision":我还尝试通过单击“编辑和部署新修订”将变量分配为环境变量:

在此处输入图像描述

For Google Cloud Run, environment variables are not set in the Dockerfile. They are set via the deployment command.对于Google Cloud Run,Dockerfile 中没有设置环境变量。它们是通过部署命令设置的。

gcloud run deploy --set-env-vars[...] gcloud run deploy --set-env-vars[...]

Tip: The EXPOSE statement also does nothing in Google Cloud Run.提示: EXPOSE语句在 Google Cloud Run 中也不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Build 替代 Google Cloud Run 环境变量 - Google Cloud Build substitutions to Google Cloud Run env vars 云构建将秘密环境传递给 dockerfile - cloud build pass secret env to dockerfile Dockerfile ENV 在容器运行期间不可用 - Dockerfile ENV not available during container run 如何在 React 中使用在 Google Cloud Run Dashboard 上声明的 ENV 变量 - How to use ENV Variables Declared on Google Cloud Run Dashboard in React GCP Cloud Run Cloud - 云 SQL 实例“${process.env.INSTANCE_CONNECTION_NAME}”不可访问 - GCP Cloud Run Cloud - Cloud SQL instance "${process.env.INSTANCE_CONNECTION_NAME}" is not reachable 将密码放在谷歌云功能环境中 - Put password on google cloud function env 在本地调试云运行时不再设置项目环境变量(如 GCP_PROJECT、CLOUDSDK_CORE_PROJECT) - Project env vars (like GCP_PROJECT, CLOUDSDK_CORE_PROJECT) are no longer set when debugging cloud run locally Google Cloud Platform:秘密作为构建环境变量 - Google Cloud Platform: secret as build env variable NextJS process.env.NEXT_PUBLIC 变量在生产中为空 - NextJS process.env.NEXT_PUBLIC variable is empty in production Firebase 使用 env 文件时拒绝访问 - Firebase denies access when using env file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM