Introduce dotenv to Docker + Rails to manage environment variables

What you want to achieve

How to manage API keys and passwords that should not be published in Rails development using Docker in a separate file.

Rough order

  1. Add gem
  2. Describe the environment variables used in the project in the .env file.
  3. Include .env file in .gitignore
  4. Pass environment variables according to erb template

Version used

procedure

Added gem'dotenv-rails'

Gemfile


gem 'dotenv-rails'

Do bundle install in the container

bash


docker-compose run container name bundle install

Edit docker-compose.yml

By writing env_file in yml, you can set to read the file that manages environment variables.

docker-compose.yml


code:docker-compose.yml
 web:
abridgement
 	env-file:
 		- .env(Describe the file name for which you want to manage environment variables)

Build a container

I edited docker-compose.yml, so I will build it again.

docker-compose build

Write the environment variables you want to set in the .env file

.env


 MAP_API_KEY="Issued API key"
 PASSWORD="password"

Add .env file to gitignore

.gitignore


 /.env

Pass as an environment variable in view's erb template

ruby:index.html.erb


Omitted above
 <script src="https://maps.googleapis.com/maps/api/js?key=<%=ENV['MAP_API_KEY']%>&callback=initMap" async defer></script>

The important thing here is to write <% = ENV ['']%> according to the erb template. Without doing this, I wrote ENV ['MAP_API_KEY'] and handed it over, and I couldn't display it in view well, and it was stuck for about 2 days.

Recommended Posts

Introduce dotenv to Docker + Rails to manage environment variables
Pass environment variables to docker container
Migrate existing Rails 6 apps to Docker environment
How to build Rails 6 environment with Docker
Rails Docker environment construction
(Basic authentication) environment variables in rails and Docker
[Rails] How to build an environment with Docker
How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)
[Environment variables] What are rails environment variables?
[Docker] Rails 5.2 environment construction 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
Rails + MySQL environment construction with Docker
Build environment with vue.js + rails + docker
Until you introduce fonts to Rails
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
How to introduce jQuery in Rails 6
[Environment construction with Docker] Rails 6 & MySQL 8
Deploy Rails on Docker to heroku
Super beginner builds Rails6 + Postgresql environment with Docker to the end
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
I tried to introduce CircleCI 2.0 to Rails app
[Docker] Use environment variables in Nginx conf
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Introduce two-factor authentication to your Rails application
[Docker] Building an environment to use Hugo
Deploy to heroku with Docker (Rails 6, MySQL)
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Rails] Assigning variables from controller to JavaScript
[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)
Preparing to introduce jQuery to Ruby on Rails
[Rails 5.x] How to introduce free fonts
Show Better Errors in Rails + Docker environment
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to use environment variables in RubyOnRails
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Complete roadmap for building environment up to Docker + rails6 + MySQL + bootstrap, jquery
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Build a development environment for Docker + Rails6 + Postgresql
How to link Rails6 Vue (from environment construction)
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails6.0 ~ How to create an eco-friendly development environment
Introduce docker to the application you are creating
[Rails] How to use PostgreSQL in Vagrant environment
Easy to display hello world with Rails + Docker
Temporarily move Docker environment from Mac to AWS
[Rails] How to easily introduce slick (slider function)
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
[Rails] Introduce Carrierwave
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
Docker environment construction
Migration error after Activerecord association in Rails5 + Docker environment (2)