LoginSignup
3
3

More than 5 years have passed since last update.

Dockerを使ってRails5を動かす

Last updated at Posted at 2017-08-06

dockerのセットアップは、こちらを見て下さい

bash
vagrant ssh

in Vagrant

bash
vi Dockerfile 
Dockerfile
FROM centos
MAINTAINER taro yamada <taro.yamada@bibo.com.ph>
RUN yum install -y git openssl-devel readline-devel zlib-devel bzip2 make gcc gcc-c++ sqlite-devel
ADD create_app.sh /usr/local/bin/create_app.sh
RUN chmod u+x /usr/local/bin/create_app.sh && /usr/local/bin/create_app.sh
ADD init.sh /usr/local/bin/init.sh
RUN chmod u+x /usr/local/bin/init.sh
EXPOSE 3000
CMD ["/usr/local/bin/init.sh"]
bash
vi create_app.sh
create_app.sh
#!/bin/bash -x
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
cd ~/.rbenv/plugins/ruby-build && ./install.sh
rbenv install 2.4.1
rbenv rehash
rbenv global 2.4.1
gem install rails
cd /root
rails new bibo
cd bibo
echo "gem 'therubyracer', platforms: :ruby" >> Gemfile
bundle install
rails g scaffold user name:string age:integer
rake db:migrate
bash
vi init.sh 
init.sh
#!/bin/bash -x
cd /root/bibo
source ~/.bash_profile
rails s -b 0.0.0.0 -p 3000 -d
exec /bin/bash

Imageの作成

bash
sudo docker build -t rails .

containerの3000をvagrantの8080に転送する用に設定して起動

bash
sudo docker run -p 8080:3000 -t rails

これでbrowserからアクセスできるはずです。

browser
http://192.168.55.44:8080/users

アクセスできない場合は、以下の項目を確認して下さい。

Vagrant上でFirewallを無効

bash
systemctl stop firewalld

こう表示されたらOK

bash
systemctl status firewalld
# Active: inactive (dead)

Vagrant上で8080がListenされているか確認

bash
ss -natu | grep 8080
#tcp    LISTEN     0      128      :::8080                 :::* 

containerが動いているか確認

bash
sudo docker ps
#9eb2ad0c6e9d rails "/usr/local/bin/in..." 1 minutes ago Up 1 minutes 0.0.0.0:8080->3000/tcp   gracious_almeida
3
3
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
3
3