LoginSignup
0
0

More than 3 years have passed since last update.

DockerでRailsの環境構築をする

Posted at

初心者がDockerでRailsの環境構築をしたのでメモとして残します。

Dockerのインストール

Dockerの公式ホームページからget startedをクリックしてDocker Desktopをクリックしダウンロードします。

Dockerの公式ホームページ https://www.docker.com/

ダウンロード後インストールが完了したらターミナルで
docker run hello-worldと打ちます。
以下のように表示されたらインストール成功です。

ターミナル
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

ファイル設定

新規ディレクトリを作成しそこに以下の4つのファイルをコピーします。
https://github.com/sekine617/Rails-file

新しいRailsプロジェクトのファイル作成

ターミナルで以下のコマンドを打ちます。

ターミナル
docker-compose run web rails new . --force --database=mysql

docker-compose run webはDockerのwebサービスコンテナで右のコマンドを実行するためのものです。
rails new .で新しいRailsプロジェクトのファイル作成し、
--forceは既存のファイルの上書き、
--database=mysqlはデータベースにMySQLを使用するコマンドです。

Gemのインストールや新規作成されたファイルをDockerに取り込むために以下のコマンドを打ちます。

ターミナル
docker-compose build

ファイル設定2

作成したファイルの/config/database.ymlを編集します。

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: ********
  host: *****

17~18行目にあるpasswordとhostを/docker-compose.ymlのpasswordとhostの値と一致させます。

/Gemfileも編集します。
gem 'mysql2', '>= 0.3.18', '<= 8.0.22'
の部分の'<= 8.0.22'を自身のMySQLのバージョンに合わせてください。

/Gemfile
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '<= 8.0.22'
# Use Puma as the app server
gem 'puma', '~> 3.0'

コンテナの起動

以下のコマンドで現在のディレクトリでコンテナの起動します。

ターミナル
docker-compose up -d

起動の確認を次のコマンドで行います。

ターミナル
docker-compose ps

以下のように表示されれば正しく起動されています。

ターミナル
   Name                  Command               State           Ports         
-----------------------------------------------------------------------------
rails_db_1    docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp   
rails_web_1   bundle exec rails s -p 300 ...   Up      0.0.0.0:3000->3000/tcp

データベースの作成

以下のコマンドでデータベースの作成します。

ターミナル
docker-compose run web bundle exec rake db:create

rake db:createでデータベースが作成されていない場合新規に作成されます。
これでRailsサーバーにアクセスできます。

サーバーへアクセス

ブラウザのURLからlocalhost:3000と入力します。
スクリーンショット 2020-12-04 21.58.04.png

Yay!You're on Rails!
と表示されればうまくアクセスできています。

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