首页 > 解决方案 > 使用 docker compose 的 ReactJS 环境变量

问题描述

这是我的 reactjs 前端 docker compose 部分,我在其中添加了 2 个环境变量。创建的变量在反应应用程序中不可用,但我看到该变量是在前端 docker 容器内创建的。有没有办法解决这个问题?

反应 Docker 撰写

webapp:
    image: webapp:latest
    build:  
        context: ./webapp
        dockerfile: Dockerfile
    volumes:
      - ./webapp:/app/webapp
      - node-modules:/app/webapp/node_modules
    ports:
      - "3000:3000"
    environment:
      - REACT_APP_ASPNETCORE_URL=localhost:5000
      - REACT_APP_ASPNETCORE_ENVIRONMENT=production
    depends_on:
      - dockerapi
    networks:
      - dockerapi-dev

我的 Docker 文件

#stage1 - build react app 
FROM node:14.15.1 as build

# set the working direction
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package*.json ./
COPY yarn*.lock ./

RUN yarn --silent
COPY . /app
RUN yarn build

# stage 2 - build the final image and copy the react build files
FROM nginx:1.17.8-alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d

EXPOSE 3000

CMD ["nginx", "-g", "daemon off;"]

使用命令“docker exec entryexit_webapp_1 env”反应容器环境变量

在此处输入图像描述

但是当我打开应用程序并转到网络详细信息时,没有设置环境变量

在此处输入图像描述

标签: reactjsdockerdocker-composeenvironment-variables

解决方案


推荐阅读