Skip to content

Instantly share code, notes, and snippets.

@takesato
Last active September 1, 2022 12:22
Show Gist options
  • Save takesato/e66318540f744bcdbf43c111a89777cf to your computer and use it in GitHub Desktop.
Save takesato/e66318540f744bcdbf43c111a89777cf to your computer and use it in GitHub Desktop.
docker と docker-compose と dotenv と.
SAMPLE1=.env_file
SAMPLE2=.env_file
SAMPLE3=.env_file

dotenv と Dockerfile と docker-compose.yml に環境変数設定するとどうなるか

公式的にはこう https://docs.docker.jp/compose/environment-variables.html

複数のファイルで同じ環境変数がある場合、Compose は使用する値を選ぶため、以下の優先度で使います。

  1. Compose ファイル
  2. シェル環境変数
  3. 環境変数ファイル
  4. Dockerfile
  5. 変数が定義されていない

environment の設定パターン

env 1 2 3 4 5
docker-comopse の environment o
docker-comopse の environment に key だけ指定 o
docker-comopse の env_file o o o
Dockerfile o o o o
.env o o o o o

dockerl-compose 経由した場合

$ docker-compose up 
Attaching to env_sample-sample-1
env_sample-sample-1  | ****************************************************************************************************
env_sample-sample-1  | before Dotenv.load
env_sample-sample-1  | SAMPLE1: environment
env_sample-sample-1  | SAMPLE2: .env
env_sample-sample-1  | SAMPLE3: .env_file
env_sample-sample-1  | SAMPLE4: dockerfile
env_sample-sample-1  | SAMPLE5: 
env_sample-sample-1  | ****************************************************************************************************
env_sample-sample-1  | after Dotenv.load
env_sample-sample-1  | SAMPLE1: environment
env_sample-sample-1  | SAMPLE2: .env
env_sample-sample-1  | SAMPLE3: .env_file
env_sample-sample-1  | SAMPLE4: dockerfile
env_sample-sample-1  | SAMPLE5: .env

docker-compose 経由しないで普通に起動した場合

> bundle exec ruby sample.rb 
****************************************************************************************************
before Dotenv.load
SAMPLE1: 
SAMPLE2: 
SAMPLE3: 
SAMPLE4: 
SAMPLE5: 
****************************************************************************************************
after Dotenv.load
SAMPLE1: .env
SAMPLE2: .env
SAMPLE3: .env
SAMPLE4: .env
SAMPLE5: .env
version: '3.7'
services:
sample:
build: .
env_file: ./.env_file
environment:
- SAMPLE1=environment
- SAMPLE2
tty: true
stdin_open: true
FROM ruby:latest
ENV SAMPLE1 dockerfile
ENV SAMPLE2 dockerfile
ENV SAMPLE3 dockerfile
ENV SAMPLE4 dockerfile
COPY . /workspace/
WORKDIR /workspace
RUN bundle install
ENTRYPOINT [ "bundle", "exec" ]
CMD [ "ruby" , "sample.rb" ]
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "dotenv"
require 'bundler'
Bundler.require
puts '*' * 100
puts 'before Dotenv.load'
puts "SAMPLE1: #{ENV['SAMPLE1']}"
puts "SAMPLE2: #{ENV['SAMPLE2']}"
puts "SAMPLE3: #{ENV['SAMPLE3']}"
puts "SAMPLE4: #{ENV['SAMPLE4']}"
puts "SAMPLE5: #{ENV['SAMPLE5']}"
Dotenv.load
puts '*' * 100
puts 'after Dotenv.load'
puts "SAMPLE1: #{ENV['SAMPLE1']}"
puts "SAMPLE2: #{ENV['SAMPLE2']}"
puts "SAMPLE3: #{ENV['SAMPLE3']}"
puts "SAMPLE4: #{ENV['SAMPLE4']}"
puts "SAMPLE5: #{ENV['SAMPLE5']}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment