0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Rails + docker-compose + volume mount + dotenv gem の組み合わせで docker コンテナでは存在しないはずの環境変数が rails console や rspec で突如として現れキャッシュされているように思える怪奇現象と立ち向かっていた

Last updated at Posted at 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

チャットメンバー募集

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

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?