LoginSignup
2
0

More than 3 years have passed since last update.

【Rails/Docker/mysql】ホストPC直下で開発していたWEBアプリをコンテナで動かしてみたぉ

Last updated at Posted at 2020-07-10

はじめに

Udemyの動画でDockerを学んだ後、ローカルホストで開発していたアプリケーションをコンテナで動かしてみました。

以下を参考にしながら、まずはdocker-composeを使わずにやってみました。

参考

udemy 米国AI開発者がゼロから教えるDocker講座
https://hub.docker.com/_/rails
https://qiita.com/tatsuya-miyamoto/items/08bd6ea142d02708614f
https://qiita.com/y-suna/items/e52b3af1d80c52b66b31
https://qiita.com/Masato338/items/f162394fbc37fc490dfb

実行環境

アプリケーションサーバ(コンテナ直下): puma4.3.3 (rails5.2.4.1 / ruby 2.5.1)
Database(ホスト直下): MySQL 5.6.47
コンテナ(ホスト直下): docker 19.03.8
ホスト: macOS Catalina 10.15.5

実施手順

既存アプリケーションを壊さないよう、対象となるアプリケーションのフォルダをコピーして実行しました。

$ cp -r ~/project/memo-space ~/project/memo-space_v2

次の通りフォルダをコピー
 ~/project/memo-space -> ~/project/memo-space_v2

Dockerfileを作成する。

Dockerfile
FROM ruby:2.5.1
RUN apt-get update
RUN apt-get install -y mysql-client nodejs vim --no-install-recommends
RUN rm -rf /var/lib/apt/lists/*
RUN mkdir /myproject
WORKDIR /myproject
ADD Gemfile /myproject/Gemfile
ADD Gemfile.lock /myproject/Gemfile.lock
RUN gem install bundler
ADD . /myproject

Gemfileを作成する

Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5.2'

Dockerfileに基づきビルド

terminal
$ cd ~/projects/memo-space_v2
$ docker build .

docker image を確認します。

terminal
 docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
<none>                         <none>              df22a2c4c7f7        56 minutes ago      1.07GB
...

dockerを実行します。
この時、ホストのrails pumaのポート3000番とmysqlのポート3306番にコンテナのポートを紐づけます。
また、ホストのアプリケーションフォルダ「~/projects/memo-space_v2」にコンテナのフォルダ「/myproject」をマウントします。

terminal
$ docker run -it -p 3000:3000 -p 3306:3306 -v ~/projects/memo-space_v2:/myproject df22a2c4c7f7 bash

ファイル「Gemfile.lock」を削除します。

terminal
$ rm Gemfile.lock

bundle installを実行し、必要なGemをインストールします。

terminal
$ bundle install

database.ymlに記載された既存のアプリの定義を次の通り修正します。

修正箇所

・mysqlのhost(追記)
・アプリケーション名(修正)

config/database.yml
# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: memo-space_v2_development   #修正
  host: docker.for.mac.localhost        #追記

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: memo-space_v2_test          #修正

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: memo-space_v2_production   #修正
  adapter: postgresql
  # database: postgresql
  encording: unicorde
  pool: 5
  username: memo-space
  url: <%= ENV['DATABASE_URL'] %>
  password: <%= ENV['MEMO-SPACE_DATABASE_PASSWORD'] %>

※既存アプリでは、元々production環境として、heroku上のpostgresqlを利用しており上記の記載となっていますが、未修正です。

※コンテナからホストPCへのアクセスは、次の通り指定することで実行できました。

terminal
mysql --host=docker.for.mac.localhost -u root

dbをcreateします。

terminal
rails db:create

dbをmigrateします。

terminal
rails db:migrate

コンテナの全てのインタフェースにバインディングすることで、ホストPCからアクセスできるようにします。

terminal
rails s -p 3000 -b '0.0.0.0'

以上により、ホストPCからブラウザでURL「localhost:3000」を指定し、アプリにアクセスできました。

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