Answers: <p>There is stages in gitlab ci and on 'docker build' stage I don't need ALL env variables. Usually its only a REACT_APP_SERVER_URI variable.
  Later I use env file in compose.yml like you are. </p>
  
  <pre><code>env_file:
    - ./envs/common.env
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-01T05:50:45.367Z
Answers: Thank you, but how do you fill those env files with CI system?
Answers: ahtohio
Answers: CC BY-SA 4.0
Answers: 2019-10-01T06:29:20.890Z
Answers: Manually =) Because developers always need to change them. So the simpliest way is do it directly on host.
Answers: Vasiliy  Shakhunov
Answers: CC BY-SA 4.0
Answers: 2019-10-01T07:29:53.477Z
Answers: Is this a right way? I mean, most of env vars don't change often and with all CI automation having to manually create some env files in production is weird.
Answers: ahtohio
Answers: CC BY-SA 4.0
Answers: 2019-10-01T17:02:03.753Z
Answers: I think automation is compromise between complexity and profit from it. Real secure deployment requires something like Hashicorp Vault cluster installation. I didn't see good secret managent solution in Chef automation. Maybe Ansible does.
Answers: Vasiliy  Shakhunov
Answers: CC BY-SA 4.0
Answers: 2019-10-01T17:07:40.823Z
Answers: Docker secrets also requires to put environment variables into the file on disk and then share it on containers. So maybe its not bad to create that file manually once.
Answers: Vasiliy  Shakhunov
Answers: CC BY-SA 4.0
Answers: 2019-10-01T17:13:49.587Z
Answers: So, maybe you could add  whole file with env variables into gitlab ci variables and then renew real env file by shell runner. https://docs.gitlab.com/ee/ci/variables/#variable-types
Answers: Vasiliy  Shakhunov
Answers: CC BY-SA 4.0
Answers: 2019-10-03T06:20:51.117Z
Answers: Creating env files from gitlab ci variables has it's own deficiencies, but nothing can be done here. Oh well, I hoped for some well-known way or practice. Thank you for your patience.
Answers: ahtohio
Answers: CC BY-SA 4.0
Answers: 2019-10-01T03:47:24.123Z
Answers: 2019-10-01T03:47:24.123Z
Answers: Vasiliy  Shakhunov
Body: <p>In our project we are using Docker and we have multiple services with a pile of environmental variables (mostly credentials to various dbs, mail, samba, 30+ entries in total). It all worked well with <code>docker_compose.yml</code> and <code>docker-compose up -d --build</code>. The advantage of this method was an extensive usage of env files. For example, I had mupltiple env files and I could use them in <code>docker-compose.yml</code>:</p>
  
  <pre><code>env_file:
    - ./envs/common.env
    - ./envs/mail.env
    - ./envs/mysql.env
  </code></pre>
  
  <p>We set up a Gitlab CI and are planning to use Docker Swarm, so no more manual creation of production env files, use CI/CD. And the primary way in Gitlab is to create variables one by one.  We could create file type variable and put the contents of env file in it, but it will be hard to manage those inner-variables. </p>
  
  <p>So how do I have to work with env vars now? </p>
  
  <ul>
  <li>Create a ton of variables in Gitlab UI and aggregate them in env files?</li>
  <li>Use file type variables?</li>
  <li><p>Pass them to services in <code>docker-compose.yml</code> file like this:</p>
  
  <p>environment:</p>
  
  <ul>
  <li>VAR1</li>
  <li>VAR2
  ...</li>
  <li>VAR20</li>
  </ul></li>
  </ul>
  
  <p>Or should we also use Docker secrets or some configuration management tool? </p>
  
Tags: <docker><docker-compose><gitlab><docker-swarm>
Title: How to properly manage mupltiple environmental variables with Gitlab CI and Docker Swarm?
UserDisplayName: ahtohio
ThreadText: # How to properly manage mupltiple environmental variables with Gitlab CI and Docker Swarm?
  
  (Asked by: username_0 on 2019-09-30)
  
  In our project we are using Docker and we have multiple services with a pile of environmental variables (mostly credentials to various dbs, mail, samba, 30+ entries in total). It all worked well with `docker_compose.yml` and `docker-compose up -d --build`. The advantage of this method was an extensive usage of env files. For example, I had mupltiple env files and I could use them in `docker-compose.yml`:
  
  ```
  env_file:
    - ./envs/common.env
    - ./envs/mail.env
    - ./envs/mysql.env
  
  ```
  
  We set up a Gitlab CI and are planning to use Docker Swarm, so no more manual creation of production env files, use CI/CD. And the primary way in Gitlab is to create variables one by one. We could create file type variable and put the contents of env file in it, but it will be hard to manage those inner-variables.
  
  So how do I have to work with env vars now?
  
  * Create a ton of variables in Gitlab UI and aggregate them in env files?
  * Use file type variables?
  * Pass them to services in `docker-compose.yml` file like this:
  
    environment:
  
    + VAR1
    + VAR2
      ...
    + VAR20
  
  Or should we also use Docker secrets or some configuration management tool?
  
  **Tags:** docker, docker-compose, gitlab, docker-swarm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-10-01. Score: 0 | Accepted answer
  
  There is stages in gitlab ci and on 'docker build' stage I don't need ALL env variables. Usually its only a REACT\_APP\_SERVER\_URI variable.
  Later I use env file in compose.yml like you are.
  
  ```
  env_file:
    - ./envs/common.env
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you, but how do you fill those env files with CI system?  
  > **username_1**: Manually =) Because developers always need to change them. So the simpliest way is do it directly on host.  
  > **username_0**: Is this a right way? I mean, most of env vars don't change often and with all CI automation having to manually create some env files in production is weird.  
  > **username_1**: I think automation is compromise between complexity and profit from it. Real secure deployment requires something like Hashicorp Vault cluster installation. I didn't see good secret managent solution in Chef automation. Maybe Ansible does.  
  > **username_1**: Docker secrets also requires to put environment variables into the file on disk and then share it on containers. So maybe its not bad to create that file manually once.  
  > **username_1**: So, maybe you could add  whole file with env variables into gitlab ci variables and then renew real env file by shell runner. https://docs.gitlab.com/ee/ci/variables/#variable-types  
  > **username_0**: Creating env files from gitlab ci variables has it's own deficiencies, but nothing can be done here. Oh well, I hoped for some well-known way or practice. Thank you for your patience.  
  
  ---
  
  