[英]Gitlab-ci and docker - env variables
我的場景:Gitlab 運行器使用 docker 在 ubuntu 服務器中部署我的反應應用程序。
我的問題:我想知道如何將 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-compose 文件中將它們添加為 arguments
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
編輯
正如評論中所討論的,創建一個.env 文件,其中您有 docker-compose.yml
touch.env
並在該文件中添加變量
echo "MY_VAR_FROM_GITLAB =$MY_VAR_FROM_GITLAB" >>.env
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.