【问题标题】:Gitlab-ci and docker - env variablesGitlab-ci 和 docker - 环境变量
【发布时间】:2021-03-20 23:02:24
【问题描述】:

我的场景:Gitlab runner 使用 docker 在 ubuntu 服务器中部署我的 react 应用程序。

我的问题:我想知道如何将 gitlab 中创建的环境变量传递给 docker compose(或 dockerfile)。为了在构建应用程序时使用它们。

我的文件:

image: docker:latest
services:
    - docker:dind

stages:
    - deploy

step-develop:
    stage: deploy
    only:
        - dev
    tags:
        - dev
    script:
        - echo MY_VAR_FROM_GITLAB # This is working good.
        - sudo docker image prune -f
        - sudo docker-compose -f docker-compose.yml build --no-cache
        - sudo docker-compose -f docker-compose.yml up -d
step-production:
    stage: deploy
    only:
        - prod
    tags:
        - prod
    script:
        - sudo docker image prune -f
        - sudo docker-compose -f docker-compose-prod.yml build --no-cache
        - sudo docker-compose -f docker-compose-prod.yml up -d
#the docker compose file version
version: '3'
# you can run multiple services inside one docker compose file
# define them with their dependencies one after the other
services:
    # service 1 named react-dev
    react-dev:
        # service 1 container name
        container_name: react-dev
        build:
            # the context (working directory) is the current directory
            # change this to the directory containing the dockerfile if in a different place
            context: .
            # the dockerfile to be run
            dockerfile: Dockerfile
        # map the exposed port from the underlying service to a port exposed to the outside
        # in this case  map port 3000 exposed by create react app to also 3000
        # to be used to access the container from the outside
        ports:
            - '3000:80'
        # the mounted volumes (folders which are outside docker but being used by docker)
        # volumes:
        #     - '.:/gt-strapi-react'
        #     - '/gt-strapi-react/node_modules'
        # set the environment to development
        environment:
            - NODE_ENV=development

【问题讨论】:

    标签: docker docker-compose environment-variables gitlab-ci gitlab-ci-runner


    【解决方案1】:

    由于是 React 应用程序,您无法在运行时添加环境变量,因此您必须在构建时添加它们。为此,将它们作为参数添加到 docker-compose 文件中

    build:
      # the context (working directory) is the current directory
      # change this to the directory containing the dockerfile if in a different place
      context: .
      # the dockerfile to be run
      dockerfile: Dockerfile
      args:
        - MY_VAR_FROM_GITLAB=${MY_VAR_FROM_GITLAB}
    

    而且你还需要在dockerfile中指定

    ARG MY_VAR_FROM_GITLAB
    ENV MY_VAR_FROM_GITLAB $MY_VAR_FROM_GITLAB
    

    编辑

    如 cmets 中所述,创建一个 .env 文件,其中包含 docker-compose.yml

    touch .env

    并在该文件中添加变量

    echo "MY_VAR_FROM_GITLAB =$MY_VAR_FROM_GITLAB" >> .env

    【讨论】:

    • 你好安德烈!感谢您的回答,我按照您的建议做了,但我在我的 gitlab 控制台上得到了这个:未设置 REACT_APP_BASE_PAGE_URL 变量。默认为空字符串。未设置 REACT_APP_CMS_URL 变量。默认为空字符串。
    • 尝试删除。来自 docker-compose.yml 的环境“在 Dockerfile 中具有任何 ARG 或 ENV 设置仅在环境或 env_file 没有 Docker Compose 条目时评估”,来源:docs.docker.com/compose/environment-variables
    • 我做到了,但同样的错误。 $ sudo docker-compose -f docker-compose.yml build --no-cache 未设置 REACT_APP_BASE_PAGE_URL 变量。默认为空字符串。未设置 REACT_APP_CMS_URL 变量。默认为空字符串。
    • 我认为 docker-compose 没有看到 gitlab 变量。在这种情况下,创建一个 .env 文件 touch .env,其中有 docker-compose.yml,并在该文件中添加变量 echo "REACT_APP_BASE_PAGE_URL =$REACT_APP_BASE_PAGE_URL" >> .env。当你运行 docker-compose 它应该使用来自 .env 的变量
    • 这最后一条评论成功了!总结一下:我在 dockerfile 中添加了 ARG 和 ENV,在 docker compose 中添加了 args,在 gitlab-ci 中添加了 echo >> .env 行!
    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多