Tismon Varghese
Tismon Varghese

Reputation: 21

gitlab variable is not accessible in the docker-compose.yml file

I am trying to create a CI/CD pipeline using gitlab and now facing an issue with the gitlab variable. This is not accessible inside docker compose file.

this is my gitlab ci yml file

step-production:
  stage: production
  before_script:
    - export APP_ENVIRONMENT="$PRODUCTION_APP_ENVIRONMENT"
  only:
    - /^release.*$/
  tags:
    - release-tag
  script:
    - echo production env value is "$PRODUCTION_APP_ENVIRONMENT"
    - sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    - sudo chmod +x /usr/local/bin/docker-compose
    - sudo docker-compose -f docker-compose.prod.yml build --no-cache
    - sudo docker-compose -f docker-compose.prod.yml up -d
  when: manual

and this is my docker compose file

version: "3"

services:
    redis:
        image: redis:latest
    app:
        build:
            context: .
        environment:
            - APP_ENVIRONMENT=${PRODUCTION_APP_ENVIRONMENT}
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - ./app:/app
        ports:
             - "8000:8000"
        restart: on-failure:5
        # network_mode: "host"

Can someone help me on how to access the gitlab variable inside docker compose file ? I have spend more than a day on the same issue

Upvotes: 0

Views: 1238

Answers (1)

Tismon Varghese
Tismon Varghese

Reputation: 21

The issue has been resolved by the following method

  1. Edit the following line in gitlab ci yml file
    sudo docker-compose -f docker-compose.prod.yml build --build-arg DB_NAME=$DEVELOPMENT_DB_NAME --build-arg DB_HOST=$DEVELOPMENT_DB_HOST --no-cache
  1. Define the value of $DEVELOPMENT_DB_NAME and $DEVELOPMENT_DB_HOST in gitlab variables section

  2. In the Docker file, add ARG and ENV sections as follows

ARG DB_NAME
ARG DB_HOST

ENV DB_NAME=${DB_NAME}
ENV DB_HOST=${DB_HOST}

Make sure that no environment variables with the same name are not defined in the docker-compose yml file

That's it !!!

Upvotes: 2

Related Questions