When I installed Docker, started the container, and posted an image to check the operation, the following error occurred. I'm addicted to the swamp so I'll write down the solution.
From the conclusion, it is because ** "There are no environment variables" ** after the introduction of S3.
I set it locally and went to source ~/.zshrc
, and I was addicted to the swamp, saying that it should have been done well ... (laughs)
Why do I get an error when I set and load an environment variable locally ...
** The reason is that the environment variable cannot be set in the container **.
The concept of a container is ** "isolated space" **. In other words, after creating and starting the container, the environment is different from the environment where the server was started with the previous rails s
command.
I think you can see the solution, but you can solve it by setting environment variables in the container as well.
So, in my case, I adopted a method that can be applied to any environment, even inside the container, by setting the environment variable once.
Specifically, we introduced a gem called dotenb-rails
.
This dotenv-rails
is ** a Gem ** that manages environment variables.
By introducing this Gem and creating a .env
file in the application directory, you can manage environment variables in one file, and you can apply environment variables to all environments just by setting environment variables in that file. I will.
Gemfile
gem 'dotenv-rails'
Terminal
bundle install
You have now introduced it. Next, let's set environment variables!
Terminal
touch .env
You can either execute the above command or have it created directly in the editor. Let's create this file and describe the environment variables you want to set.
If you want to change the environment variables for each environment, the following articles will be very helpful, so please take a look.
[Rails] Let's manage environment variables by understanding how to install and use dotenv-rails!
Only the description of .env is explained here.
.env
AWS_ACCESS_KEY_ID = 'value'
AWS_SECRET_ACCESS_KEY = 'value'
When I run rails c
in the terminal, I get the following:
Terminal
irb(main):002:0> ENV['AWS_ACCESS_KEY_ID']
=> 'value'
irb(main):993:0> ENV['AWS_SECRET_ACCESS_KEY']
=> 'value'
It is okay to set only these two settings to solve the error, but if you want to set other settings, you need to add a description.
I was able to set the environment variables, but if I push it to git as it is, the contents of the environment variables will be ** published **.
Therefore, you have to add .env
to the .gitignore
file so that it will not be displayed in the commit history and prevent push.
.gitignore
#Bottom line
.env
You can have it treated as a file that does not exist from git just by writing as above.
Now that you have set the environment variables, you can use them by simply calling them where you want to use them. It's a very convenient gem, so please try it.
[Rails] Let's manage environment variables by understanding how to install and use dotenv-rails!
Recommended Posts