简体   繁体   中英

How to add variables from GitLab settings to docker-compose file for my container on the server?

I am trying to set environment variables for the server in the docker composer file for the mail client of my Django application. Before I add variables in Gitlab CI/CD settings (Rep->settings->CI/CD->variables) and try to use it in my docker-compose file

My docker-compose file:

version: '3'

services:
  my-server:
    image: server:latest
    build: .
    entrypoint: ["./entry_point.sh"]
    container_name: my-server
    environment:
      - DEBUG=True
      - DB_HOST=db
      - SUPPORT_EMAIL=${SUPPORT_EMAIL}
      - EMAIL_HOST=${EMAIL_HOST}
      - EMAIL_PORT=${EMAIL_PORT}
      - EMAIL_HOST_USER=${EMAIL_HOST_USER}
      - EMAIL_HOST_PASSWORD=${EMAIL_HOST_PASSWORD}
    volumes:
      - media:/home/app/my-project/media/
    ports:
      - "8000:8000"
    depends_on:
      - db

But where I check docker container vars I see next.

root@myserver:~# docker exec ce30fd7afa8e  bash -c 'printenv'
EMAIL_HOST=
HOSTNAME=hjf7a7578
PYTHON_VERSION=3.7.5
PWD=/home/app
HOME=/root
LANG=C.UTF-8
PIPENV_SYSTEM=1
EMAIL_HOST_USER=
EMAIL_PORT=
EMAIL_HOST_PASSWORD=
DB_HOST=db
SUPPORT_EMAIL=
DEBUG=True
_=/usr/bin/printenv

How can I fix this? I'm trying this format:

-SUPPORT_EMAIL:${SUPPORT_EMAIL}

and just:

-SUPPORT_EMAIL

But in this case, I don't even see the keys inside the container.

If you have your environment variables defined within GitLab Variables then those variables will be available on Runner level only and not on host (server where you are deploying your application).

If you need to have those variables from GitLab inside of the docker-compose file on the server where you are uploading I would suggest to you that you made some bash script which will be executed inside of Runner during the pipeline run which will replace the values in your docker-compose file. Afterwards you can upload that modified ZIP file as an Artifact and use it later on your host machine within deploying job to unzip the proper docker-compose file and use it.

Example:

so, in your .gitlab-ci.yml something like:

append_variables:
  stage: variables
  script:
    - sed -i "s|SUPPORT_EMAIL=|SUPPORT_EMAIL=${SUPPORT_EMAIL:-""}|i" docker-compose.yml
    - zip ${CI_COMMIT_SHA}.zip docker-compose.yml
    - // upload artifact here either to GitLab / JFrog or some other Artifactory

On this way you can bring your GitLab variables to the server where application runs.

Of course, there are many other better ways to do this, eg via Ansible or etc but this is just a rough example how it can be done so that you can have a feeling about it.

You can send compose time variable into docker compose by using the '--build-arg' flag. You can access it into the Dockerfile as '$'. You can store the variables in the gitlab ci/cd variable section and access them via ${variable_name} and construct the compose command in the.gitlab-ci.yml.

docker-compose build --build-arg db_username="${db_username}" --build-arg db_password="${db_password}" 

is a sample docker compose command to be added to.gitlab-ci.yml while the variables are from the gitlab ci/cd variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM