1
1

More than 1 year has passed since last update.

サクッとRailsの開発環境をDockerで構築してみる

Last updated at Posted at 2021-11-14

環境

  • macOS Catalina 10.15.6
  • Docker 20.10.10
  • docker-compose 2.1.1

DockerでRailsの開発環境を構築する

公式ドキュメントを参考にDockerでRailsの開発環境を構築する。
Quickstart: Compose and Rails

ファイルを作成する

以下のファイルを作成する。

$ mkdir hello_app/
hello_app$ cd hello_app
hello_app$ touch Dockerfile
hello_app$ touch docker-compose.yml
hello_app$ touch Gemfile
hello_app$ touch Gemfile.lock
Dockerfile
# syntax=docker/dockerfile:1
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

# 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

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  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
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5'

Gemfile.lockは空のまま。

Gemfile.lock

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 "$@"

プロジェクトを作成する

hello_app$ docker-compose run --no-deps web rails new . --force --database=postgresql
hello_app$ ls -ltotal 80
-rw-r--r--   1 {name}  staff   483 11 14 20:15 Dockerfile
-rw-r--r--   1 {name}  staff  2214 11 14 20:27 Gemfile
-rw-r--r--   1 {name}  staff  5236 11 14 20:27 Gemfile.lock
-rw-r--r--   1 {name}  staff   374 11 14 20:27 README.md
-rw-r--r--   1 {name}  staff   227 11 14 20:27 Rakefile
drwxr-xr-x@ 10 {name}  staff   320 11 14 20:27 app/
drwxr-xr-x@  9 {name}  staff   288 11 14 20:27 bin/
drwxr-xr-x@ 16 {name}  staff   512 11 14 20:27 config/
-rw-r--r--   1 {name}  staff   130 11 14 20:27 config.ru
drwxr-xr-x@  3 {name}  staff    96 11 14 20:27 db/
-rw-r--r--   1 {name}  staff   358 11 14 20:27 docker-compose.yml
-rw-r--r--@  1 {name}  staff   201 11 14 20:24 entrypoint.sh
drwxr-xr-x@  4 {name}  staff   128 11 14 20:27 lib/
drwxr-xr-x@  3 {name}  staff    96 11 14 20:27 log/
-rw-r--r--   1 {name}  staff    63 11 14 20:27 package.json
drwxr-xr-x@  9 {name}  staff   288 11 14 20:27 public/
drwxr-xr-x@  3 {name}  staff    96 11 14 20:27 storage/
drwxr-xr-x@ 11 {name}  staff   352 11 14 20:27 test/
drwxr-xr-x   6 {name}  staff   192 11 14 20:27 tmp/
drwxr-xr-x@  3 {name}  staff    96 11 14 20:27 vendor/

DBの設定を変更する

config/database.yml を以下の内容で書き換える。

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

DBを作成する

hello_app$ docker-compose run web rake db:create
[+] Running 1/0
 ⠿ Container hello_app-db-1  Running                                          0.0s
Created database 'myapp_development'
Created database 'myapp_test'

アプリケーションを起動する

hello_app$ docker-compose build
hello_app$ docker-compose up

画面を確認する

http://localhost:3000/ をブラウザから閲覧する。

以下の画面が表示されれば成功です。

スクリーンショット 2021-11-14 20.40.21.png

PG:ConnectionBadと怒られたら

スクリーンショット 2021-11-14 20.34.46.png

DBを作成出来ていない可能性があるので作成する。

DBを作成する

1
1
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
1
1