简体   繁体   English

docker compose with create react app procedure 未定义的环境变量

[英]docker compose with create react app procedure undefined environment variables

I have a problem when I declare environment variables in the docker-compose.yml file, but when I try to read them I get "undefined".我在 docker-compose.yml 文件中声明环境变量时遇到问题,但是当我尝试读取它们时,我得到“未定义”。

My steps:我的步骤:

I created a React app with "create react app", then I ran "eject" (but not changed anything yet).我用“create react app”创建了一个 React 应用程序,然后我运行了“eject”(但还没有改变任何东西)。 Also, I added Ngix to the docker.另外,我将 Ngix 添加到 docker。

Then I created a Dockerfile and added it to the root:然后我创建了一个 Dockerfile 并将其添加到根目录:

FROM node:alpine

WORKDIR '/app'

COPY ./package.json ./

RUN npm i

COPY . .

CMD ["npm", "run", "start"]

Then I created the docker-compose.yml file:然后我创建了 docker-compose.yml 文件:

version: '3'
services:
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile
      context: ./nginx
    ports:
      - '3050:80'
  client:
    build:
      context: ./client
      dockerfile: Dockerfile
      args:
        - MAXIMUM_CAMERAS_COUNT=9
    volumes:
      - /app/node_modules
      - ./client:/app

Then i do "docker-compose up --build" and it works fine.然后我做“docker-compose up --build”,它工作正常。
But when I do somewhere in the app:但是当我在应用程序中的某个地方执行以下操作时:

console.log('process.env.MAXIMUM_CAMERAS_COUNT:', process.env.MAXIMUM_CAMERAS_COUNT);

I get "undefined".我得到“未定义”。

Already tried:已经尝试过:
-Add to Dockerfile: - 添加到 Dockerfile:

ARG MAXIMUM_CAMERAS_COUNT
ENV MAXIMUM_CAMERAS_COUNT $MAXIMUM_CAMERAS_COUNT

and in the docker-compose.yml:在 docker-compose.yml 中:

  args:
    - MAXIMUM_CAMERAS_COUNT=9

-Add to compose-docker.yml: - 添加到 compose-docker.yml:
 environment: - MAXIMUM_CAMERAS_COUNT=9

-Add to compose-docker.yml: - 添加到 compose-docker.yml:
 environment: MAXIMUM_CAMERAS_COUNT: 9

Any other ideas?还有其他想法吗?

如本文档中所述,您需要在变量前加上REACT_APP_前缀,因此在您的情况下,它将是REACT_APP_MAXIMUM_CAMERAS_COUNT

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM