[RUBY] How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)

Introduction

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.

aaaaa.png

Solutions

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.

Introduction of Gem doten b-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.

1) Described in Gemfile

Gemfile


gem 'dotenv-rails'

Terminal


bundle install

You have now introduced it. Next, let's set environment variables!

2) Create .env and 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.

3) Description in .gitignore

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.

4) Use of environment variables

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.

References

[Rails] Let's manage environment variables by understanding how to install and use dotenv-rails!

Recommended Posts

How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)
Pass environment variables to docker container
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
How to set environment variables when using Payjp with Rails
How to set and describe environment variables using Rails zsh
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
[Rails] How to use rails console with docker
Migrate existing Rails 6 apps to Docker environment
How to resolve errors when installing Rails 5.1.3
Show Better Errors in Rails + Docker environment
How to use environment variables in RubyOnRails
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to migrate a web application created in a local docker environment to AWS
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to check the logs in the Docker container
How to deploy a container on AWS Lambda
How to link Rails6 Vue (from environment construction)
How to get a heapdump from a Docker container
(Basic authentication) environment variables in rails and Docker
How to update pre-built files in docker container
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to use PostgreSQL in Vagrant environment
Temporarily move Docker environment from Mac to AWS
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to build docker environment with Gradle for intelliJ
List how to learn from Docker to AKS on AWS
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to download images from AWS S3 (rails, carrierwave)
[Docker + Rails] How to deal with Rails server startup failure
How to write Rails
Rails Docker environment construction
How to install Docker
How to uninstall Rails
Solution for errors that occur when upgrading to Rails 5.2 series
How to get inside a container running on AWS Fargate
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
[Docker context] ~ How to access docker in remote environment from VScode ~
How to deploy a Rails application on AWS (article summary)
[Docker] How to access the host from inside the container. http://host.docker.internal:
Connect to AWS RDS from your local PC Docker environment
How to build an environment with Docker, which is the minimum required to start a Rails application
[rails] How to post images
[AWS] How to check logs
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
[Environment variables] What are rails environment variables?
How to set Docker nginx
How to use rails join
[Docker] Rails 5.2 environment construction with docker
How to terminate rails server
How to write Rails validation
How to write Rails seed