4

I'm not familiar with Docker or devops, but I have these files:

.env.production

DO_NOT_SEND_EMAILS=false

docker-compose.ci.yml

services:
  my-app:
    environment:
      - DO_NOT_SEND_EMAILS=true
  • Both files contain more than that, but these are the relevant lines.
  • What will the envvar be set to in the end?
miken32
  • 42,008
  • 16
  • 111
  • 154
Mirror318
  • 11,875
  • 14
  • 64
  • 106

3 Answers3

11

It seems like dotenv does not override variables if they are defined in the environment, by design:

By default, it won't overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use Dotenv.overload.

So the answer probably depends on how you are using dotenv - Dotenv.load or Dotenv.overload.

Here is a minimal test:

.env

SOMEVAR=from .env file
ANOTHERVAR=also from .env file

docker-compose.yml

version: '3'

services:
  test:
    build: .
    command: ruby test.rb
    volumes:
    - .:/app
    environment:
      SOMEVAR: from docker compose

Dockerfile

FROM dannyben/alpine-ruby
WORKDIR /app
RUN gem install dotenv
COPY . .

test.rb

require 'dotenv'

# values in .env will override
# Dotenv.overload

# values in .env will be used only if not already set in the environment
Dotenv.load

p ENV['SOMEVAR']
p ENV['ANOTHERVAR']

To run:

$ docker-compose build
$ docker-compose run test
DannyB
  • 12,810
  • 5
  • 55
  • 65
2

As i know, it's not supposed to overwrite. Personally I use in docker env.

    env_file:
      - '.env.production'

instead of:

    environment:
      - DO_NOT_SEND_EMAILS=true

In this case always will rely on .env.prod file...

7urkm3n
  • 6,054
  • 4
  • 29
  • 46
0

As pointed out by DannyB dotenv doesn't override existing environment variables by default. For anyone using the dotenv CLI, you can override already existing environment variables using the --override or -o option like this:

dotenv -o -f ".env.local,.env"
amit_saxena
  • 7,450
  • 5
  • 49
  • 64