LoginSignup
7
7

More than 5 years have passed since last update.

Docker初心者のRuby開発環境構築

Posted at

はじめに

インフラの知識がなくても楽して環境が作成できると聞いたので使ってみたが、
実際にはかなり苦労したので自分なりにまとめました。

環境:centos7、Docker18.06.1
※Docker for Windowsでも確認済み

Dockerの流れ

  1. Docker HubまたはDockerfileからイメージを取得
  2. イメージからコンテナを作成・実行

zu4.png

イメージ?コンテナ??

イメージが湧かないので身近なものに当てはめて例えてみました。

VirtualBoxで例えてみた

  • イメージ → スナップショット
  • コンテナ → 仮想マシン

スナップショットからスナップショット取得時点の仮想マシンを作成
同じ状態の仮想マシンを複数作成可能

zu2.png

ゲームで例えてみた

  • イメージ → セーブデータ
  • コンテナ → ゲーム機+ゲームソフト

セーブデータからセーブ時点のゲームを再開
同じゲーム状態を複数端末で作成可能

zu3.png

本題

Dockerを理解するために3種類の方法で環境構築を実施

1. centOSのイメージから自分でRuby開発環境を構築

・イメージの取得、コンテナの作成・実行

docker pull centos
docker run -it --name centosRuby centos /bin/bash --login

・ここからはLinuxの話
以下の記事を参考にRubyをインストールしていく。
CentOS 7.3にRuby 2.4.1をインストールする

★rbenvのインストール
root@00:/# yum install -y git
root@00:/# git clone https://github.com/rbenv/rbenv.git ~/.rbenv
root@00:/# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
root@00:/# ~/.rbenv/bin/rbenv init
root@00:/# source ~/.bash_profile

★ruby-buildのインストール
root@00:/# git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
root@00:/# ~/.rbenv/plugins/ruby-build/install.sh

★Rubyのインストール
root@00:/# yum install -y openssl-devel readline-devel zlib-devel 
root@00:/# yum remove bzip2
root@00:/# yum install -y bzip2 gcc make
root@00:/# rbenv install 2.5.0
root@00:/# echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
root@00:/# source ~/.bash_profile
root@00:/# rbenv global 2.5.0

・Rubyファイルの作成

root@00:/# vi hello.rb
hello.rb
puts "Hello World"

・Hello World 出力

root@00:/# ruby hello.rb 
Hello World

2. Dockerfileからイメージを作成

・1.の操作を元に作成したDockerfileをGitHubから取得する。
https://github.com/manchan13/centos_ruby25

ll -tr
合計 12
-rw-r--r-- 1 test test  42  9月 24 23:17 hello.rb
-rw-r--r-- 1 test test  16  9月 24 23:17 README.md
-rw-r--r-- 1 test test 962  9月 24 23:17 Dockerfile

・docker buildでイメージを作成

docker build -t centos_ruby_dockerfile .

・作成したイメージの確認

docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
centos_ruby_dockerfile           latest              46fa6e609251        2 minutes ago       555MB

・コンテナ作成・実行

docker run -it --name centosRubyDockerfile centos_ruby_dockerfile /bin/bash --login

・Heloo World 出力

[root@00 /]# cd ~/
[root@00 ~]# ruby hello.rb 
hello world!hello world!!

3. Rubyのofficialイメージから作成

これが一番楽ちん

・イメージ取得、コンテナの作成・実行

docker pull ruby
docker run -it ruby /bin/bash

・rubyイメージにはvimが入ってないのでそのままHello World 出力

root@00:/# ruby -e 'puts "Hello World"'
Hello World
7
7
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
7
7