在Google云运行中使用Dockerfile时,ReactJS进程.env.NODE_ENV未设置为生产

31moq8wy  于 2022-11-28  发布在  Docker
关注(0)|答案(1)|浏览(131)

我有一个ReactJS网站,我正在谷歌云运行使用Dockerfile。
Google Cloud Run中的网站仍然将变量NODE_ENV显示给开发人员,即使我已经在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"]

登录名.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>
  )
}

"这给了"

Please Log in development

我还尝试通过单击“Edit and deploy new Revision”(编辑并部署新修订版本)将变量分配为环境变量:

gfttwv5a

gfttwv5a1#

对于Google Cloud Run,环境变量不是在Dockerfile中设置的,而是通过部署命令设置的。
......[详细]
提示:EXPOSE语句在Google Cloud Run中也没有任何作用。

相关问题