简体   繁体   English

在 Dockerfile 中初始化 react.env 变量的更简洁方法

[英]Cleaner way to init react .env variables in Dockerfile

Is there another way to init react env in the dockerfile?在 dockerfile 中是否有另一种方法来初始化反应环境?

FROM node:14.19 as base

ARG REACT_APP_VERSION
ARG REACT_APP_WEBSITE_NAME
ARG REACT_APP_BACKEND_BASE_URL
ARG REACT_APP_BI_BASE_URL
ARG REACT_APP_DEFAULT_DEPARTEMENT_CODE

ENV REACT_APP_VERSION $REACT_APP_VERSION
ENV REACT_APP_WEBSITE_NAME $REACT_APP_WEBSITE_NAME
ENV REACT_APP_BACKEND_BASE_URL $REACT_APP_BACKEND_BASE_URL
ENV REACT_APP_BI_BASE_URL $REACT_APP_BI_BASE_URL
ENV REACT_APP_DEFAULT_DEPARTEMENT_CODE $REACT_APP_DEFAULT_DEPARTEMENT_CODE

WORKDIR /app

COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm install
COPY . .

FROM base as development
CMD ["npm", "start"]

FROM base as build
RUN npm run build

FROM nginx:alpine as production
COPY nginx.conf /etc/nginx/conf.d/configfile.template
COPY --from=build /app/build /usr/share/nginx/html
ENV HOST 0.0.0.0
EXPOSE 8080
CMD sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

Actually I have all variables declared at the top on the Dockerfile and I have to pass each.env variable in arg .实际上,我在 Dockerfile 的顶部声明了所有变量,我必须在arg中传递 each.env 变量。 But I don't like this way.但我不喜欢这种方式。 I'm searching another way to do that.我正在寻找另一种方法来做到这一点。 I'm using cloud build for my pipeline production deployment so maybe there is a way to do something with it?我正在为我的管道生产部署使用云构建,所以也许有办法用它做些什么?

You don't need to replicate all your args as env, if they are only required for the build process.如果仅在构建过程中需要它们,则无需将所有 args 复制为 env。 They will be seen as env variable during build.它们将在构建期间被视为环境变量。 Afterwards, when you run the container they are gone though.之后,当您运行容器时,它们就消失了。

This is enough, for the first stage:对于第一阶段,这就足够了:

FROM node:14.19
ARG REACT_APP_VERSION="unknown" \
    REACT_APP_WEBSITE_NAME="" \
    REACT_APP_BACKEND_BASE_URL="" \
    REACT_APP_BI_BASE_URL="" \
    REACT_APP_DEFAULT_DEPARTEMENT_CODE="" \
    NODE_ENV="production"
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build

For your second stage you can use nginx templates dir, it will do envsubst for you and copy the templates into the conf.d directory, removing the template suffix.对于第二阶段,您可以使用 nginx 模板目录,它将为您执行envsubst并将模板复制到conf.d目录,删除模板后缀。

It will run the following script amongst others: https://github.com/nginxinc/docker-nginx/blob/master/entrypoint/20-envsubst-on-templates.sh它将运行以下脚本: https://github.com/nginxinc/docker-nginx/blob/master/entrypoint/20-envsubst-on-templates.sh

So you can create templates with variable placeholders and put them in /etc/nginx/templates .因此,您可以创建带有可变占位符的模板并将它们放在/etc/nginx/templates中。

In your case, you want to put /etc/nginx/templates/default.conf.template .在您的情况下,您想放置/etc/nginx/templates/default.conf.template

FROM nginx:alpine
ENV HOST 0.0.0.0 PORT=8080
EXPOSE 8080
COPY nginx.conf /etc/nginx/templates/default.conf.template
COPY --from=0 /app/build /usr/share/nginx/html

That way, you can also keep the default entrypoint and command.这样,您还可以保留默认入口点和命令。 You don't have to set your own.您不必自己设置。

I would also suggest using the unprivileged version of the nginx. It runs on 8080 by default.我还建议使用 nginx 的非特权版本。它默认在 8080 上运行。 https://hub.docker.com/r/nginxinc/nginx-unprivileged . https://hub.docker.com/r/nginxinc/nginx-unprivileged This image is generally useful if you plan to drop capabilities, which is advised for production.如果您计划放弃功能,建议将此映像用于生产环境,此映像通常很有用。

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

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