I tried to build Rails6 and Docker environment, so I will leave it here as a memorandum. Qiita This is my first post, and since I am a beginner, I think there are various points that cannot be reached, so I would appreciate it if you could point out any mistakes.
environment
reference Build Rails6 + MySQL environment with docker-compose [Rails environment construction] Environment construction with docker + rails + mysql (even beginners can complete in 30 minutes!) [ENTRYPOINT is "must execute", CMD is "(default) argument"](https://pocketstudio.net/2020/01 / 31 / cmd-and-entrypoint /)
It was carried out with reference to this.
Build the environment and display the Rails home screen.
https://hub.docker.com/ Go to and install.
$ mkdir ~/Desktop/docker-tutrial/Rails_app
$ cd ~/Desktop/docker-tutrial/Rails_app
$ code Dockerfile
Open a terminal, create a directory in any location, and write `` `Dockerfile``` as follows. (I use vsCode.)
$code file name
Then vsCode will start and you can write a new file. (VsCode must be installed on your PC in advance.)
FROM ruby:2.7
RUN apt-get update -qq && apt-get install -y nodejs yarnpkg
RUN ln -s /usr/bin/yarnpkg /usr/bin/yarn
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
source 'https://rubygems.org'
gem 'rails', '~>6'
$ touch Gemfile.lock
Write the initial `Gemfile`
and the empty `` `Gemfile.lock```.
Entrypoint defined as ENTRYPOINT in Dockerfile.Describe sh.
```shell
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
version: '3'
services:
db:
image: mysql:8.0
volumes:
- ./tmp/db:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=1
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
MySQL uses version 8.0
By setting ** MYSQL_ALLOW_EMPTY_PASSWORD **, you can connect as root even if the password is empty.
$ docker-compose run web bundle exec rails new . --force --database=mysql
Overwrite the existing file with ** --force ** and specify MySQL with ** --database **. Now that you have the usual Rails fileset, build it.
$ docker-compose build
Since the database connection cannot be made as it is, Replace the ** host ** part of ** config / database.yml ** with ** db **.
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: db
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
** db ** is the container name.
Once this is done, build again and do ** docker-compose up **. If you access Rails with ** localhost: 3000 **, you will get ** ActiveRecord :: NoDatabaseError **.
This happens because the web container does not support the ** caching_sha2_password authentication method ** of mysql 8.0. Follow the steps below to change to the old authentication method ** mysql_native_password **.
First of all, enter the DB container and start bash.
$ docker-compose exec db bash
Then connect with the mysql command.
# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL
...
You can see the user list and its authentication method by sending the following query.
mysql> select User,Host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
Change ** caching_sha2_password ** in the rightmost plugin to ** mysql_native_password **. Use ** ALTER USER ** to change the target ** root @% ** user settings this time.
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '';
-- Query OK, 0 rows affected (0.02 sec)
After change
mysql> select User,Host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
If you do ** db: prepare ** and create a DB, you will be able to access the Rails home screen.
Recommended Posts