I had a hard time building the environment with Rails6 and MySQL with Docker, so I will leave a memorandum in the article as well.
As a stumbling block Unlike Rails5, Rails6 has required the introduction of Webpacker, a gem package for using Webpack with Ruby on Rails, which strongly supports modern front-end development.
Create a folder with any name (it doesn't have to be a command)
mkdir dockerSampleApp 
Create a file in the folder created in 1.
Dockerfiledocker-compose.ymlGemfileGemfile.lockentrypoint.shDockerfile
Regardless of the folder name of the project, the part of myapp can be left as myapp.
Dockerfile
FROM ruby:2.7
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
    && apt-get update -qq \
    && apt-get install -y nodejs yarn \
    && mkdir /myapp
    
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
docker-compose.yml
version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
volumes:
  mysql-data:
    driver: local
Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
Gemfile.lock
Gemfile.lock
#Do not write anything in this file
entrypoint.sh
entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Execute command
$ docker-compose run web rails new . --force --no-deps --database=mysql --skip-test --webpacker --api
About options used this time
-- --force Overwrite existing Gemfile
-- --no-deps Do not start the linked service
-- --database = mysql Specify MySQL for the database
-- --skip-test Skip the installation of Minitest (because the test will introduce RSpec)
--"--webpackerInstall webpacker (required package management tool for Rails 6)  ―― --api`` Since I want to make only API, execute it in API mode. As a result, unnecessary View / UI related libraries will not be installed.
Image build means installing various dependent libraries and middleware, and installing and setting your own applications.
Execute command
$ docker-compose build
Corrected the relevant part of  database.yml.
Corrected the relevant part according to the settings of  services and  MYSQL_ROOT_PASSWORD in  docker-compose.yml.
config/database.yml
# ~abridgement~
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password  # ->Correct the blank to password
  host: db  # ->Fixed from localhost to db
# ~abridgement~
Execute the command to create a DB
$ docker-compose run web rails db:create
Finally execute the command to start the container
$ docker-compose up
It may take some time, but it's OK if such a log flows in the terminal
web_1  | => Booting Puma
web_1  | => Rails 6.0.3.4 application starting in development 
web_1  | => Run `rails server --help` for more startup options
web_1  | Puma starting in single mode...
web_1  | * Version 4.3.6 (ruby 2.7.2-p137), codename: Mysterious Traveller
web_1  | * Min threads: 5, max threads: 5
web_1  | * Environment: development
web_1  | * Listening on tcp://0.0.0.0:3000
web_1  | Use Ctrl-C to stop
When I access  localhost: 3000, the page is displayed safely. Should be! !!
I managed to build an environment! In the future, I would like to learn about infrastructure such as Docker and deepen my understanding!
I was very helpful!
Thank you very much! !!
Recommended Posts