Building a Docker environment on EC2 (ArgumentError: Missing `secret_key_base`, Mysql2 :: Error :: ConnectionError: Access denied for user'root'@ '172.18.0.4' (using password: NO) solution)

Introduction

When I tried to build an infrastructure using EC2 instance, I was addicted to the following two error swamps when creating a DB, so I will explain the flow to solve it and create a DB for deployment by EC2 using Docker / docker-compose. ..

** Error ① **

Terminal


$ docker-compose run web rails db:create RAILS_ENV=production

ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit’

** Error ② **

Terminal


$ docker-compose run web rails db:create RAILS_ENV=production

Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.18.0.4' (using password: NO)

Development environment

Ruby 2.6.5 Bundler 2.1.4 Rails 6.0.0 MySQL 5.6.50

Premise

In performing the work in this article, it is assumed that Docker and docker-compose are installed in the development environment / EC2.

Here, I will explain only the installation of Docker and docker-compose on EC2.

Docker installation on EC2

The work will be done based on Docker Basics-Amazon EC2 Container Service.

Next, install Docker.

Terminal(In EC2)


#yum update
$ sudo yum update -y

#Install docker from yum
$ sudo yum install -y docker

#Start docker service
$ sudo service docker start
Starting cgconfig service:                                 [  OK  ]
Starting docker:                                           [  OK  ]

# ec2-Add user to docker group
$ sudo usermod -a -G docker ec2-user

Log out and log in again to use the docker command.

Terminal(In EC2)


$ docker info

Please refer to [Docker] ERROR: Could n’t connect to Docker daemon at http + docker: // localhost – is it running? for why to add it to the group.

Installing Docker Compose on EC2

We will work according to Install Docker Compose --Docker Documentation.

Terminal(EC2)


#Temporarily become superuser
$ sudo -i

#Temporarily superuser from here
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# docker-Grant execute permission to compose command
$ chmod +x /usr/local/bin/docker-compose

#Exit superuser
$ exit

# docker-Confirm execution of compose command
$ docker-compose --version
docker-compose version 1.11.2, build dfed245

DB creation in EC2 Docker

From here, I proceeded with reference to I tried to build a docker environment on ec2.

First, create a docker container and start it in the background.

Terminal(In EC2)


#Container creation(Start in background)
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose up -d 

#Check if it is running
[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker ps -a

If it can be started, it is finally time to create the DB of the main subject.

Be sure to commit → push ** remote changes before creating, and ** apply them to your production environment as well. ** **

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ git pull origin master

Next, we will create a DB.

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:create RAILS_ENV=production

If it becomes as follows, it is successful.

Terminal(In EC2)


created database 'app name_production'

If all goes well, it will look like this, but in my case, I got the following error.

Error ① ArgumentError: Missing secret_key_base for'production' environment, set this string with` rails credentials: edit'

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:create RAILS_ENV=production

ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit’

To resolve this error, I referred to (AWS) ArgumentError: Missing'secret_key_base' for'production' environment, set this string with'rails credentials: edit'.

The cause of the error seems to occur because the file config/credentials.yml.enc is not in the local environment and the production environment.

So it seems that it can be solved by deleting and recreating the credentials.yml.enc file once in ** production environment (EC2) **.

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ cd /var/www/app name/config

[ec2-user@ip-XXX-XX-XX-XXX config]$ ls
application.rb       database.yml    locales    storage.yml
boot.rb              environment.rb  puma.rb    unicorn.rb
cable.yml            environments    routes.rb  webpack
credentials.yml.enc #This file
initializers    spring.rb  webpacker.yml

[ec2-user@ip-XXX-XX-XX-XXX config]$ rm credentials.yml.enc
#Delete with rm command

#Let's check that it has been deleted with the ls command

If you can delete it safely, let's recreate it.

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX config]$ EDITOR=vim rails credentials:edit

Adding config/master.key to store the encryption key: 54xx0x9xx86x4x754x9x0xxxxx10x492

Save this in a password manager your team can access.

If you lose the key, no one, including you, can access anything encrypted with it.

      create  config/master.key

File encrypted and saved.

#Success if create and save are done

Alright, this is the solution! !! Create a DB! !! When I executed the command to create, I got the following error.

Error ② Mysql2 :: Error :: ConnectionError: Access denied for user'root'@ '172.18.0.4' (using password: NO)

To summarize the error content, you are angry with ** "I don't have the password to log in to the DB" **.

At this point, I deleted the Docker container, stopped / restarted it, and recreated it, but I couldn't solve it and got hooked on the swamp (laughs).

The cause was the description of the production environment (production) described in database.yml.

That should be the case, because I changed the setting of database.yml to introduce Docker.

config/database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

#Change settings from here
  username: <%= ENV.fetch('DB_USERNAME', 'root') %>
  password: <%= ENV.fetch('DB_PASSWORD', 'password') %>
  socket: /tmp/mysql.sock
  host: <%= ENV.fetch('DB_HOST', 'db') %>

.env


DB_USERNAME='root'
DB_PASS='password'
DB_HOST='db'
MYSQL_ROOT_PASS='password'

See here for environment variable settings in .env.

The local settings are as above, but in the production environment they are as follows.

config/database.yml


production:
  <<: *default
  database:app name_production
  username: root
  password: <%= ENV['DATABASE_PASSWORD'] %>
  socket: /var/lib/mysql/mysql.sock

In other words, with the description as it is, there is a difference between the local database and the production database, and it is not possible to create a database. You can resolve this error by combining this production description with your local description.

config/database.yml


production:
    <<: *default
    database: tumlog_production
    host: <%= ENV.fetch('DB_HOST', 'db') %>
    username: <%= ENV.fetch('DB_USERNAME', 'root') %>
    password: <%= ENV.fetch('DB_PASSWORD', 'password') %>
    socket: /var/lib/mysql/mysql.sock

After rewriting as above, commit → push ** with git and execute ** git pull origin master ** in the terminal.

Terminal(In EC2)


[ec2-user@ip-XXX-XX-XX-XXX repository name]$ git pull origin master
* branch            master     -> FETCH_HEAD
   6f883e9..99c894d  master     -> origin/master
Updating 6f883e9..99c894d
Fast-forward
 config/database.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

[ec2-user@ip-XXX-XX-XX-XXX repository name]$ docker-compose run web rails db:create RAILS_ENV=production

created database 'app name_production'

As mentioned above, the DB was created in the production environment. Lastly, it's easy to forget ** git pull origin master **, so forget about it and don't get stuck in the error swamp yourself (laughs).

References

I tried to build a docker environment on ec2Install Docker, Docker Compose on AWS EC2 Amazon Linux(AWS) ArgumentError: Missing 'secret_key_base' for 'production' environment, set this string with 'rails credentials:edit'

Recommended Posts

Building a Docker environment on EC2 (ArgumentError: Missing `secret_key_base`, Mysql2 :: Error :: ConnectionError: Access denied for user'root'@ '172.18.0.4' (using password: NO) solution)
Mysql2 :: Error :: ConnectionError: Access denied for user'root' @'localhost' (using password: YES)
Access denied for user'root' @'localhost' (using password: NO) Corrective action for error
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Access denied for user'root' @'localhost' (using password: YES) and gave up
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Building a Ruby environment for classes on Mac
A memo when building a Rails 5.2 development environment using Docker Desktop + WSL2 on Windows 10 Home
[For beginners] Until building a Web application development environment using Java on Mac OS
[2021] Build a Docker + Vagrant environment for using React / TypeScript
Solution for the error "no basic auth credentials" when pushing a Docker container to Heroku