簡體   English   中英

在 docker-compose.yml 文件中使用環境變量

[英]Using environment variables in docker-compose.yml file

我正在嘗試配置 GitLab CI 以使用 Docker 和 Docker Compose 持續構建和部署我的應用程序。

運行 CI 管道時,我收到以下錯誤消息:

The Compose file './docker-compose.ci.yml' is invalid because:
services.dashboard.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports value [':', ':', ':'] has non-unique elements

我想使用環境變量來隱藏我的配置。

以下是我的docker-compose.ci.yml的片段:

version: "3.9"

services:
  dashboard:
    build:
      context: ./dashboard
      dockerfile: Dockerfile.prod
      cache_from:
        - "${BACKEND_IMAGE}"
    image: "${BACKEND_IMAGE}"
    command: gunicorn dashboard.wsgi:application --bind ${DJANGO_HOST}:${DJANGO_PORT}
    volumes:
      - static_volume:/home/app/web/static
    ports:
      - "${DJANGO_PORT}:${DJANGO_PORT}"
    env_file:
      - .env
    depends_on:
      - postgres

...

  mosquitto:
    build: 
      context: ./mosquitto
      cache_from:
        - "${MOSQUITTO_IMAGE}"
    image: "${MOSQUITTO_IMAGE}"
    volumes:
      - ./mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mosquitto/data:/mosquitto/data
      - ./mosquitto/log:/mosquitto/log
      - broker_certs:/mosquitto/config/certs
    ports:
      - "${MQTT_DEFAULT_PORT}:${MQTT_DEFAULT_PORT}"
      - "${MQTT_SECURE_PORT}:${MQTT_SECURE_PORT}"
      - "${MQTT_WEBSOCKETS_PORT}:${MQTT_WEBSOCKETS_PORT}"
    env_file:
      - .env
...

在我的構建階段,我使用 bash 腳本設置環境變量:

.gitlab-ci.yml

image:
  name: docker/compose:1.28.5
  entrypoint: [""]

services:
  - docker:dind

stages:
  - build

variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2

build:
  stage: build
  before_script:
    ...
    - chmod +x ./setup_env.sh
    - bash ./setup_env.sh
    ...
...

setup_env.sh

...

# mosquitto config
echo MQTT_DEFAULT_PORT=$MQTT_DEFAULT_PORT >> .env
echo MQTT_SECURE_PORT=$MQTT_SECURE_PORT >> .env
echo MQTT_WEBSOCKETS_PORT=$MQTT_WEBSOCKETS_PORT >> .env
echo MOSQUITTO_COMMON_NAME=$MOSQUITTO_COMMON_NAME >> .env

...

我所有的變量都在 Gitlab 上設置得很好。

在我的機器上本地運行docker-compose不會產生任何錯誤。

我究竟做錯了什么?

上面顯示的配置是正確的。 問題是我的分支沒有受到保護,並且 Gitlab 上的環境變量僅為受保護的分支/標簽設置。 因此,它們從未在構建過程中被選中。 如果有人遇到類似問題,請確保您的分支/標簽受到保護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM