0
0

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.

【Ruby on Rails】rails newコマンドについて

Last updated at Posted at 2022-09-17

PostgreSQLでrails newしたい場合

rails new <appname> -d postgresql

Dockerでrails newする場合

まずはDockerfileを作成。

FROM ruby:2.7
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs npm postgresql-client
RUN npm install --global yarn
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 4000
CMD ["rails", "server", "-b", "0.0.0.0"]

ドキュメントの内容をversion以外そのまま。
https://docs.docker.jp/compose/rails.html

次にGemfileを。

source 'https://rubygems.org'
gem 'rails', '6.1.6.1'

空のGemfile.lockを作成

touch Gemfile.lock

最後にdocker-compose.ymlを作成

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 4000 -b '0.0.0.0'"
    stdin_open: true
    tty: true
    volumes:
      - .:/myapp
    ports:
      - "4000:4000"
    depends_on:
      - db
entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /cuon_cms/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

rails newをする。

docker-compose run web rails new . --force --database=postgresql

雛形ができるが、DBの設定がまだなので、行う。

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

docker compose upを実行したときに、
webpackerのエラーが出たら、

docker-compose run web rails webpacker:install
docker-compose run web rails webpacker:compile

DBを作成

docker-compose run web rails db:create

確かRails7だとこんなエラーは出ずにすんなり行けた気がするが...

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?