1
1

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

【環境構築】Docker rails 備忘録

1
Last updated at Posted at 2021-01-27

Docker使ってrailsでアプリを作成した際の備忘録

手順1

ファイル作成
既にDockerをインストールしているので
使用しているディレクトリの配下に任意の名前でディレクトリを作成し
以下作成したディレクトリにファイル準備。
・Gemfile
・Gemfile.lock
・Dockerfile
・docker-compose.yml
※ターミナルでのファイル作成コマンド:touch XXXXXX(※XXXに作成するファイル名)

手順2

作成したファイルの内容を編集

Gemfile
source 'https://rubygems.org'
gem 'rails', 5.2.2
Dockerfile
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /sample
WORKDIR /sample

COPY Gemfile /sample/Gemfile
COPY Gemfile.lock /sample/Gemfile.lock

RUN bundle install
COPY . /sample
docker-compose.yml
version: '3'

services:
    db:
        image: mysql:5.7
        environment:
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: password
        ports:
            - "3306:3306"
        volumes:
            - ./db/mysql/volumes:/var/lib/mysql

    web:
        build: .
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes:
            - .:/sample
            - gem_data:/usr/local/bundle
        ports:
            - 3000:3000
        depends_on:
            - db 
        tty: true
        stdin_open: true

volumes:
  gem_data:

手順3

Dockerコマンドを実行し、railsアプリケーションを作成
※Dockerを起動していない場合は次のコマンドを実行:
docker run -d -p 80:80 docker/getting-started

・rails new コマンド(DBにmysqlを指定)
docker-compose run web rails new . --force --database=mysql --skip-bundle

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?