🖥

Rails Trouble : Rails console read dotenv file - .env - for docker com

2019/12/05に公開

Conclusion

  • .env file copied from host and exist in docker container ( BAD CASE )
  • "dotenv" gem read .env with rails console and rspec
  • not exestence env in docker container appears in rails!

HEY DO NOT DO COPY FUZZY with Dockerfile

What was the trouble

is this the trouble dependent on my environment or code ?

.env

ABC=XXX

docker-compose up

.env file effect for build docker-compose file
it for not docker container ENV

docker container

Yes so not set ABC env in container

root@c0d7a2a32aec:/app# env | grep ABC
root@c0d7a2a32aec:/app#

but ...

Try rails console

oh whats happen!?

[1] pry(main)> ENV['ABC']
=> "xxx"

Try Ruby pry

there is not ENV['ABC']
it seems rails only trouble

[1] pry(main)> ENV['ABC']
=> nil

I noticed

rails read .env file ?
this docker container BAD copy
copy all current directory files

COPY . .

So .env file is in docker container

root@432f3ef01b16:/app# cat .env
ABC=xxx

Try remove .env

And rerun rails console

root@349934df34c4:/app# rm .env

specify do not use spring cache

root@349934df34c4:/app# DISABLE_SPRING=1 bundle exec rails console
[1] pry(main)> ENV['ABC']
=> nil
[2] pry(main)>

ENV leave!

Reason

Gemfile

group :development, :test do
  gem "dotenv-rails"

Ah I found maybe

Finally i tried

i remove gem "dotenv-rails" in Gemffile
bundle install

and Try rails console

( and .env file is in docker container )

[2] pry(main)> ENV['ABC']
=> "xxx"

🎉

Conclusion

Do not copy to container .env file !

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2808

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-12-05

Discussion