[英]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.