1
2

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 3 years have passed since last update.

dockerで脳死環境構築 rails 6

Posted at

自分用メモです。

dockerでのreils環境構築

https://qiita.com/me-654393/items/ac6f61f3eee66380ecd7
これをもろパクリしました。

#ターミナル
$ mkdir myapp
$ cd myapp
$ code .
$ touch Dockerfile //中身を記載
$ touch Gemfile //中身を記載
$ touch Gemfile.lock
$touch entrypoint.sh //中身を記載
$ touch docker-compose.yml //中身を記載

#ファイル内コピペする
##Dockerfile

FROM ruby:2.6.6

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

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# 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

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

##Gemfile

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

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

##docker-compose.yml

version: '3'
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

#ターミナル

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

$ docker-compose build

#ちょいめんどいやつ
データベースの情報を設定するために、config/database.ymlを変更し、コマンドでDBを作成する。

##config/database.yml

# 設定箇所のみ抜粋
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password # docker-compose.ymlのPOSTGRES_PASSWORDで指定した値
  pool: 5
:
:
development:
  <<: *default
  database: myapp_development
:
:
test:
  <<: *default
  database: myapp_test

#ターミナル

$ docker-compose run web rake db:create

$ docker-compose up

成功!
http://localhost:3000

よく使うコマンド

$ docker ps 起動中のコンテナを確認

$ docker-compose down コンテナ消す

$ docker ps -aq | xargs docker rm コンテナ全消し

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?