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