Docker로 Rails6+MySQL 개발 환경 구축
필요한 파일/디렉토리 준비
touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,.env}
  % tree -L 1
.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── docker-compose.yml
DockerfileFROM ruby:2.7.2
ENV LANG C.UTF-8
ENV APP_ROOT /app
WORKDIR /app
RUN apt-get update \
  && 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 \
  && apt-get install -y sqlite3 \
  && apt-get install -y libsqlite3-dev \
  && apt-get install -y vim \
  && apt-get clean  \
# Capybaraでテストするために必要なものをインストール
# 署名を追加(chromeのインストールに必要) -> apt-getでchromeと依存ライブラリをインストール
  && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add \
  && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | tee /etc/apt/sources.list.d/google-chrome.list \
  && apt-get update -qq \
  && apt-get install -y google-chrome-stable libnss3 libgconf-2-4 \
  && CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` \
  && curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip \
  && unzip /tmp/chromedriver_linux64.zip \
  && mv chromedriver /usr/local/bin/ \
# Ruby-2.7以降だと`gem install bundler`しないとエラーが発生する
  && gem install bundler
RUN mkdir -p $APP_ROOT
WORKDIR $APP_ROOT
COPY Gemfile $APP_ROOT/Gemfile
COPY Gemfile.lock $APP_ROOT/Gemfile.lock
RUN bundle update --bundler \
&& bundle install  --jobs 4 --retry 3
docker-compose.ymlversion: '3'
services:
  db:
    image: mysql:8.0.20
    volumes:
      - ./tmp/db:/var/lib/mysql
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    env_file: .env
  web:
    build: .
    command: /bin/sh
    env_file: .env
    environment:
      WEBPACKER_DEV_SERVER_HOST: "0.0.0.0"
      RAILS_SERVE_STATIC_FILES: "1"
      EDITOR: "vim"
    volumes:
      - ./app:/app:cached
    ports:
      - "3000:3000"
    depends_on:
      - db
    tty: true
.envMYSQL_ROOT_PASSWORD=password
TZ=Japan
Gemfilesource 'https://rubygems.org'
ruby '2.7.2'
gem 'rails', '6.0.3.4'
Gemfile.lock# Gemfile.lockは空
이미지 만들기
첫 번째 터미널에서 다음 명령을 실행합니다:.
$ docker-compose build --no-cache
$ docker-compose up
  Rails 프로젝트 작성
다른 터미널에서 다음을 수행합니다.
$ docker-compose exec web bash
/app# rails -v
Rails 6.0.3.4
/app# rails new . --force --database=mysql --skip-turbolinks --skip-test
/app# bin/rails webpacker:install
--skip-test:test를 만들지 않습니다.--skip-test-unit와 동일--force: 파일이 있더라도 덮어쓰기config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch('MYSQL_USERNAME') { 'root' } %>
  password: <%= ENV.fetch('MYSQL_PASSWORD') { 'password' } %>
  host: <%= ENV.fetch('MYSQL_HOST') { 'db' } %>
development:
  <<: *default
  database: app_development
test:
  <<: *default
  database: app_test
production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
데이터베이스를 만듭니다./app# bin/rails db:create
/app# bin/rails db:migrate
 서버를 시작합니다./app# bin/rails s -b 0.0.0.0
 브라우저에서 http://localhost:3000/ 를 엽니다.정부의 방법은 아래와 같다.
시작 코드: Compose 및 Rails
RSpec 설치
Gemfile 및 rspec 및 factorybot의gem를 추가합니다.
Gemfile
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end
Rails 응용 프로그램에서 Rspec을 가져옵니다./app# bundle exec rails g rspec:install
Running via Spring preloader in process 1096
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb
                Reference
이 문제에 관하여(Docker로 Rails6+MySQL 개발 환경 구축), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/fjsh/articles/430b40112633cd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)