【发布时间】:2019-02-04 16:29:30
【问题描述】:
我正在构建一个 React 应用程序并使用 Docker 和 Jenkins 进行生产部署...如何安全地为这个静态应用程序提供环境变量? 这是我的 Dockerfile:
# stage 1: build the react app
FROM node:10.15.0 as react-build
WORKDIR /app
COPY . /app
ARG REACT_APP_API_ENTRYPOINT
ARG REACT_APP_CONNECT_URI
ARG REACT_APP_CONNECT_CLIENT_ID
ARG REACT_APP_CONNECT_SECRET
ARG REACT_APP_CONNECT_CALLBACK_URL
RUN yarn
ENV NODE_ENV=production
ENV REACT_APP_API_ENTRYPOINT=${REACT_APP_API_ENTRYPOINT}
ENV REACT_APP_CONNECT_URI=${REACT_APP_CONNECT_URI}
ENV REACT_APP_CONNECT_CLIENT_ID=${REACT_APP_CONNECT_CLIENT_ID}
ENV REACT_APP_CONNECT_SECRET=${REACT_APP_CONNECT_SECRET}
ENV REACT_APP_CONNECT_CALLBACK_URL=${REACT_APP_CONNECT_CALLBACK_URL}
RUN yarn run -s build
# Stage 2: build the production environment
FROM openresty/openresty:alpine
COPY deploy/files/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=react-build /app /usr/local/openresty/nginx/html
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
EXPOSE 8000
ENTRYPOINT ["nginx", "-g", "daemon off;"]
目前构建过程使这些变量暴露:(
【问题讨论】:
-
为什么要在 React 等客户端应用中使用一些秘密变量?
-
因为我需要连接到需要它的 API。保护这些变量的最佳做法是什么?
-
您是否有后端应用程序可以连接到 API?通常,当您连接到 API 时,您需要在后端进行连接,并且可以保护您的秘密变量。
标签: reactjs docker jenkins docker-compose yarnpkg