[英]Env vars lost when building docker image from Gitlab CI
I'm trying to build my React / NodeJS project using Docker and Gitlab CI.我正在尝试使用 Docker 和 Gitlab CI 构建我的 React / NodeJS 项目。
When I build manually my images, I use.env file containing env vars, and everything is fine.当我手动构建图像时,我使用包含 env vars 的 .env 文件,一切都很好。
docker build --no-cache -f client/docker/local/Dockerfile . -t espace_client_client:local
docker build --no-cache -f server/docker/local/Dockerfile . -t espace_client_api:local
But when deploying with Gitlab, I can build successfully the image, but when I run it, env vars are empty in the client.但是使用 Gitlab 部署时,我可以成功构建映像,但是当我运行它时,客户端中的环境变量为空。
Here is my gitlab CI:这是我的 gitlab CI:
image: node:10.15
variables:
REGISTRY_PACKAGE_CLIENT_NAME: registry.gitlab.com/company/espace_client/client
REGISTRY_PACKAGE_API_NAME: registry.gitlab.com/company/espace_client/api
REGISTRY_URL: https://registry.gitlab.com
DOCKER_DRIVER: overlay
# Client Side
REACT_APP_API_URL: https://api.espace-client.company.fr
REACT_APP_DB_NAME: company
REACT_APP_INFLUX: https://influx-prod.company.fr
REACT_APP_INFLUX_LOGIN: admin
REACT_APP_HOUR_GMT: 2
stages:
- publish
docker-push-client:
stage: publish
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY_URL
image: docker:stable
services:
- docker:dind
script:
- docker build --no-cache -f client/docker/prod/Dockerfile . -t $REGISTRY_PACKAGE_CLIENT_NAME:latest
- docker push $REGISTRY_PACKAGE_CLIENT_NAME:latest
Here is the Dockerfile for the client这是客户的 Dockerfile
FROM node:10.15-alpine
WORKDIR /app
COPY package*.json ./
ENV NODE_ENV production
RUN npm -g install serve && npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD [ "serve", "build", "-l", "3000" ]
Why is there such a difference between the 2 process?为什么2个过程有这么大的区别?
According to your answer in comments, GitLab CI/CD environment variables doesn't solve your issue.根据您在评论中的回答, GitLab CI/CD 环境变量不能解决您的问题。 Gitlab CI environment is actual only in context of GitLab Runner that builds and|or deploys your app.
Gitlab CI 环境仅在构建和/或部署您的应用程序的GitLab Runner的上下文中实际存在。
So, if you are going to propagate Env vars to the app, there are several ways to deliver variables from .gitlab-cy.yml
to your app:因此,如果您要将 Env 变量传播到应用程序,有几种方法可以将变量从
.gitlab-cy.yml
到您的应用程序:
ENV
instruction Dockerfile ENV
指令Dockerfile Eg例如
FROM node:10.15-alpine
WORKDIR /app
COPY package*.json ./
ENV NODE_ENV production
ENV REACT_APP_API_URL: https://api.espace-client.company.fr
ENV REACT_APP_DB_NAME: company
ENV REACT_APP_INFLUX: https://influx-prod.company.fr
ENV REACT_APP_INFLUX_LOGIN: admin
ENV REACT_APP_HOUR_GMT: 2
RUN npm -g install serve && npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD [ "serve", "build", "-l", "3000" ]
environment
directive environment
指令web:
environment:
- NODE_ENV=production
- REACT_APP_API_URL=https://api.espace-client.company.fr
- REACT_APP_DB_NAME=company
- REACT_APP_INFLUX=https://influx-prod.company.fr
- REACT_APP_INFLUX_LOGIN=admin
- REACT_APP_HOUR_GMT=2
run -e
run -e
(Not your case, just for information)
(不是你的情况,仅供参考)
docker -e REACT_APP_DB_NAME="company"
There is convenient way to store variables outside of your code: Custom environment variables有一种方便的方法可以在代码之外存储变量: 自定义环境变量
You can set them up easily from the UI .您可以从 UI 轻松设置它们。 That can be very powerful as it can be used for scripting without the need to specify the value itself.
这可能非常强大,因为它可以用于编写脚本而无需指定值本身。
(source: gitlab.com ) (来源: gitlab.com )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.