简体   繁体   English

从 Gitlab CI 构建 docker 图像时环境变量丢失

[英]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" ]

docker-compose environment directive docker-compose 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

Docker run -e Docker run -e

(Not your case, just for information) (不是你的情况,仅供参考)

docker -e REACT_APP_DB_NAME="company"

PS Try Gitlab CI variables PS 试试 Gitlab CI 变量

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.

相关问题 Gitlab CI 正在转义我的 docker-compose.yml 文件中的环境变量 - Gitlab CI is escaping env vars in my docker-compose.yml file 构建映像后,如何从.gitlab-ci.yml 运行我的 docker 映像? - How do I run my docker image from .gitlab-ci.yml after building the image? 使用 docker 从 gitlab ci 发布图像 - Publishing image with docker from gitlab ci 在 Z9792E23419583666B178605ZDECAEC5 中构建和推送 Spring 启动 maven 插件 docker 映像 - Building and pushing Spring Boot maven plugin docker image in Gitlab CI 在GitLab CI中为Node.js应用程序构建Docker映像 - Building a Docker image for a Node.js app in GitLab CI Gitlab CI 如何在构建 docker 镜像之前运行测试 - Gitlab CI how to run tests before building docker image Gitlab CI CD 未在我的子文件夹中构建 docker 图像 - Gitlab CI CD not building docker image in my subfolders 使用 MinGW 编译的 Qt5 构建 Docker 映像在从“docker:latest”映像运行的容器中工作,但在 GitLab CI 中失败 - Building of a Docker image with Qt5 compiled with MinGW works in a container run from "docker:latest" image, but fails in GitLab CI Gitlab-ci 和 docker - 环境变量 - Gitlab-ci and docker - env variables 在gitlab-ci Docker执行器中构建Docker映像时出现“无法连接到Docker守护程序。”错误 - “Cannot connect to the Docker daemon.” error while building docker image in gitlab-ci docker executor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM