LoginSignup
11
10

More than 5 years have passed since last update.

dockerでrailsを新規作成しようとしてハマった。

Last updated at Posted at 2015-10-14

概要

久しぶりにrailsで遊んでみようと思ったらハマったのでメモ。
※ただし、2015/10/14時点の話なので注意してください。

ゴールはproxy環境なwindowsでrailsが動くとこまで。

環境構築

前提

vagrantでcoreosにログイン済。

ruby:2.2をproxy対応

今回はrails:latestとrails:onbuildを使う。

またproxy環境なので以下のサイトのスクリプトを使用して親のイメージである「ruby:2.2」をproxy環境化。
http://qiita.com/speg03/items/4b8573686e7bbf218b61

まず新規アプリを作成したいので「rails:latest」をbuildするため以下をチェックアウトするかそのままDockerfileを作成して「docker build -t rails:latest .」とか実行。
https://github.com/docker-library/rails/blob/7926577517fb974f9de9ca1511162d6d5e000435/Dockerfile

イメージが完成したら新規アプリを作成するためにrunを実行。

docker run --rm -it -v "$(pwd)":/usr/src/app -w /usr/src/app rails rails new HOGE -d mysql
  • 実行したディレクトリがアプリを作成するディレクトリ。
  • HOGEがアプリの名称
  • DBはmysqlを指定してる。postgresqlを使いたければ-dの指定で変更可能。

ここまでうまく行ってればGemfile.lockの生成までできてるはず。

とりあえずここらでMYSQLコンテナを作成してコンテナを起動しておく。

docker pull mysql
docker run -p 3306:3306 --name testdb -e MYSQL_ROOT_PASSWORD=password -d mysql

上記コマンド実行でユーザがrootでパスワードがpasswordのmysqlコンテナが起動する。

次に開発するためにrails:onbuildを使う。
https://github.com/docker-library/rails/blob/9fb5d2b7e0f2e7029855028e07e86ab7ec54abaa/onbuild/Dockerfile
上記をチェックアウトするか直接Dockerfileを作成してビルドする。

今回は環境変数の設定もしたいので直接作成する。

FROM ruby:2.2

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ONBUILD COPY Gemfile /usr/src/app/
ONBUILD COPY Gemfile.lock /usr/src/app/
ONBUILD RUN bundle install

ONBUILD COPY . /usr/src/app

RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*

ENV DB_USER root
ENV DB_PASSWORD password
ENV DB_NAME testdb
ENV DB_ADAPTER mysql2
ENV DATABASE_URL ${DB_ADAPTER}://${DB_USER}:${DB_PASSWORD}@db/${DB_NAME}

EXPOSE 3000

環境変数に先程作成したmysqlの情報を設定。

早速、コンテナをビルド、起動する。

# Dockerfileの場所で実行
docker build -t rails:test .

# 先程作成したアプリのディレクトリに移動して実行
docker run -it -p 3000:3000 -v $(pwd):/usr/src/app --link testdb:db rails:test bash

bundle installを実行

rails consoleを起動してみる。

root@5949d24c0cac:/usr/src/app# bin/rails c
/usr/local/bundle/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `g
em 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

エラー。。。

Gemfileにmysql2も入ってるのでなんでなのか調べるとそのまんまの記事があった。
http://qiita.com/shizuma/items/0f9660d5d46a0012eb9e

mysql2のバージョンを指定すれば良いとのことなので上記の記事通りにGemfileを修正してbundle install

root@1e8ccf02841a:/usr/src/app# bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the Gemfile freeze
by running `bundle install --no-deployment`.

You have added to the Gemfile:
* mysql2 (~> 0.3.20)

You have deleted from the Gemfile:
* mysql2

またエラー。。。。

なんかbundle install --no-deploymentって書いてあるから実行。

root@1e8ccf02841a:/usr/src/app# bundle install --no-deployment
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the Gemfile freeze
by running `bundle install --no-deployment`.

You have added to the Gemfile:
* mysql2 (~> 0.3.20)

You have deleted from the Gemfile:
* mysql2

エラー。。。

Gemfile.lockが更新できないようなので関係しそうなとこ見てみるとDockerfileにこんな記述が。

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

ググッてみる。

HIT。

前にrailsやってた時はまだなかったのか使ってなかっただけなのかわからんがとりあえず解除して再度「bundle install」

bundle config --delete frozen
bundle install

動いた。

rails c

root@1e8ccf02841a:/usr/src/app# bin/rails c
Loading development environment (Rails 4.2.4)
irb(main):001:0> [*1..10].each{|n|p n}
1
2
3
4
5
6
7
8
9
10
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

rails console動いた。

dbを作成する。

bin/rake db:create

HOSTから確認。

mysql -h IP_ADDRESS -p
mysql> show databses;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| testdb             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

DBもできたー。

ちなみにIPアドレスはvagrantfile見るか、hostでifconfig実行すればわかる。

ついでにブラウザのから確認して見る。

railsコンテナの中に入って

bin/rails s -b 0.0.0.0

rails001.JPG

見れた。

ちなみに最初はrails sのみで実行したら動かなかったので以下を参考にしてバインドオプションをつけた。
参考 http://hosi.hateblo.jp/entry/2015/01/19/203316

せっかくだからなんか作ってみる。

core@core-01 ~ $ docker exec -it 1e8ccf02841a bash
root@1e8ccf02841a:/usr/src/app# bin/rake db:migrate
root@1e8ccf02841a:/usr/src/app# bin/rails g scaffold blog title:string body:string
      invoke  active_record
      create    db/migrate/20151014070727_create_blogs.rb
      create    app/models/blog.rb
      invoke    test_unit
      create      test/models/blog_test.rb
      create      test/fixtures/blogs.yml
      invoke  resource_route
       route    resources :blogs
      invoke  scaffold_controller
      create    app/controllers/blogs_controller.rb
      invoke    erb
      create      app/views/blogs
      create      app/views/blogs/index.html.erb
      create      app/views/blogs/edit.html.erb
      create      app/views/blogs/show.html.erb
      create      app/views/blogs/new.html.erb
      create      app/views/blogs/_form.html.erb
      invoke    test_unit
      create      test/controllers/blogs_controller_test.rb
      invoke    helper
      create      app/helpers/blogs_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/blogs/index.json.jbuilder
      create      app/views/blogs/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/blogs.coffee
      invoke    scss
      create      app/assets/stylesheets/blogs.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
root@1e8ccf02841a:/usr/src/app# bin/rake db:migrate
== 20151014070727 CreateBlogs: migrating ======================================
-- create_table(:blogs)
   -> 0.0703s
== 20151014070727 CreateBlogs: migrated (0.0709s) =============================

最初のmigrate必要だっけか?もう忘れてる。。。

とりあえず起動。

bin/rails s -b 0.0.0.0

http://IP_ADDRESS:3000/blogsにアクセス。

rails3.JPG

できたー。

rails consoleを触りたかっただけなのに結局時間かかった。
おかしいなぁ、dockerを使えばすぐ使えるって展開したかっただけなのにすぐ使えなかった。。。

あと新規アプリ作成するとこまでできたらdocker-compose使うようにしたほうが楽かと。

とりあえず動かすとこまで完了。

おまけ

  • コンテナへの再接続はdocker exec -it コンテナID bash
  • コンテナを起動してつなぐにはdocker start -a コンテナID
11
10
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
11
10