2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

React + Rails + AWS Fargate の構成を実現したい - 01 バックエンド環境構築編(Docker + Rails)

Last updated at Posted at 2022-08-21

概要

本記事のゴール

以下を参考にDocker + Railsの環境構築を行う。

以下つまづいた点をメモする。

つまづいた点

1. Dockerfileの修正

参考のDockerfileだとエラーが発生したので、修正

FROM ruby:2.6.6

ENV LANG C.UTF-8
ENV TZ Asia/Tokyo

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
- RUN apt-get update -qq && \
- apt-get install -y build-essential \
- libpq-dev \
- sudo \
- gem install bundler:2.0.1
+ RUN apt-get update -qq
+ RUN apt-get install -y build-essential libpq-dev
+ RUN gem install bundler:2.0.1

RUN bundle install

ADD . /app

2. config/yamlの修正

差分がわかるように以下記載

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
- host: localhost

development:
  <<: *default
+ host: db
  database: app_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
+ host: <%= ENV['DB_HOST'] || 'db' %>
  database: app_test

3. docker-compose up後

エラー内容

backend  | Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
backend  | Run `bundle install` to install missing gems.
backend exited with code 7

解決策

エラーメッセージより、以下を実行する

docker-compose run backend bundle install
docker-compose up

4. localhost:5000 アクセス後

エラー内容

ActiveRecord::NoDatabaseError (Unknown database 'app_development'):

解決策

以下でデータベースを作成してから、再度アクセスする

docker-compose up
docker-compose exec backend bin/rails db:create

エラー内容

Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development

解決策

rails db:create後にrails db:migrateを行う

docker-compose exec backend bin/rails db:create
docker-compose exec backend bin/rails db:migrate

参考
https://k-koh.hatenablog.com/entry/2020/02/06/103517

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?