Vagrantを少し触ってみた

環境

今回の環境は、以下の通りです。

OS:Windows 10
Edition:Home
Version:1809
OS Build:17763.1

インストールするものは、以下です。

Vagrant:2.1.15
VirtualBox:5.2.18

使ってみる

Vagrant と VirtualBox をインストールする

Vagrantは以下からダウンロードできます。

Explore Vagrant product documentation, tutorials, and examples.

VirtualBoxは以下から。

インストールしたら、Windowsを再起動しておきましょう。

Boxファイルの準備

Boxファイルとは、Vagrantで管理する仮想マシンのテンプレートのようなものです。
以下のサイトから、Vagrant公式のBoxを見つけます。

Vagrant Cloud by HashiCorp

「centos/7」というのがあると思いますので、これを使います。
こちらです。
https://app.vagrantup.com/centos/boxes/7

「How to use this box with Vagrant:」と書かれている箇所の「New」というタブに使い方が書かれています。

作業フォルダの準備

程よい場所に作業用フォルダを作成する。

c:> mkdir c:\Vagrant
c:> cd c:\Vagrant

続いて、仮想マシン用のフォルダを作成する。
今回は「centos7」としました。

c:\Vagrant> mkdir centos7
c:\Vagrant> cd centos7

Vagrantを初期化する。

「vagrant init」でVagrantを初期化します。
Boxページに書かれていた通り、以下のようにコマンドを実行します。

c:\Vagrant\centos7> vagrant init centos/7

仮想マシンを起動する。

Vagrantの初期化が終わったら、仮想マシンを起動します。

c:\Vagrant\centos7> vagrant up

初回実行時はboxファイルが自動的にダウンロードされ、仮想マシンが起動します。
boxファイルのダウンロードは少し時間がかかると思いますので、少し待ちましょう。

vagrant statusコマンドを使うと、仮想マシンの状態を確認できます。

c:\Vagrant\centos7> vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run <code>vagrant halt</code> to
shut it down forcefully, or you can run <code>vagrant suspend</code> to simply
suspend the virtual machine. In either case, to restart it again,
simply run <code>vagrant up</code>.

boxファイルの行方

vagrant upコマンドを実行した際に、boxファイルがダウンロードされます。
ダウンロードされたboxファイルは、以下に保存されます。

C:\Users\ユーザ名.vagrant.d\boxes

ここにbox毎のフォルダが作成されます。
もう少し深いところに、vmdkファイルがありました。

C:\Users\ユーザ.vagrant.d\boxes\centos-VAGRANTSLASH-7\1809.01\virtualbox\centos-7-1-1.x86_64.vmdk

仮想マシンにログインする

仮想マシンが起動したら、仮想マシンにログインします。

ログインユーザは「vagrant」ユーザです。
鍵認証ですのでログイン時にパスワードの入力は求められませんが、何かのタイミングでパスワードを求められた場合は「vagrant」を使いましょう。

c:\Vagrant\centos7&gt; vagrant ssh
[vagrant@localhost ~]$

Boxファイルを作ってみる

仮想マシンのCentOSの環境をいい感じにしてみる

ひとまず、パッケージをアップデート

$ sudo yum update -y

私が実行した際は、アップデート対象はありませんでした。

日本語表示できるようにパッケージを入れる。
ネットで調べてみたところ、以下のような感じでいけるかなと思いましたが、日本語表示にはなりませんでした。

$ sudo yum reinstall -y glibc-common
$ sudo yum -y install ibus-kkc vlgothic-*
$ localectl set-locale LANG=ja_JP.UTF-8
$ source /etc/locale.conf

localeコマンドを見ると、設定はされてそうなのですが。

$ locale
LANG=ja_JP.UTF-8
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=

タイムゾーンをJSTにしておく。

$ sudo cp /usr/share/zoneinfo/Japan /etc/localtime

Python 3 をインストールする

$ sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum install python36u python36u-devel python36u-libs python36u-pip
$ cd /usr/bin
$ sudo ln -s python3.6 python3
$ python3 -V
Python 3.6.5

Box を作成する

仮想マシンからログアウトして、vagrant packageコマンドでBoxを作成します。
CentOS 7 に Python 3 をインストールしたものなので、以下のような感じで centos7withpython3 というBoxを作成します。

$ exit
c:\Vagrant\centos7&gt; vagrant package default centos7withpython3 --output centos7withpython3.box

c:\Vagrant\centos7&gt; dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は FECA-6413 です

c:\Vagrant\centos7 のディレクトリ

2018/10/08  17:45    <DIR>          .
2018/10/08  17:45    <DIR>          ..
2018/10/08  16:05    <DIR>          .vagrant
2018/10/08  17:45       627,243,245 centos7withpython3.box
2018/10/08  16:05             3,145 Vagrantfile

サイズが630MBくらいのcentos7withpython3.boxが作成されました。

Box を登録する

作成したBoxファイルを登録します。

c:\Vagrant\centos7&gt; vagrant box add centos7withpython3 c:\Vagrant\centos7\centos7withpython3.box
==&gt;  box: Box file was not detected as metadata. Adding it directly…
==&gt;  box: Adding box 'centos7withpython3' (v0) for provider:
box: Unpacking necessary files from: file:///c:/Vagrant/centos7/centos7withpython3.box
box: Progress: 100% (Rate: 959M/s, Estimated time remaining: --:--:--)
==&gt;  box: Successfully added box 'centos7withpython3' (v0) for 'virtualbox'!

登録できたか確認しましょう。

c:\Vagrant\centos7&gt; vagrant box list
centos/7           (virtualbox, 1809.01)
centos7withpython3 (virtualbox, 0)

Vagrantの初期化

c:\Vagrant\centos7&gt; cd ..

c:\Vagrant&gt; mkdir centos7p3

c:\Vagrant&gt; cd centos7p3

c:\Vagrant\centos7p3&gt; vagrant init centos7withpython3
A <code>Vagrantfile</code> has been placed in this directory. You are now
ready to <code>vagrant up</code> your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
<code>vagrantup.com</code> for more information on using Vagrant.

仮想マシンの起動

では、起動させます。

c:\Vagrant\centos7p3&gt; vagrant up

状態を確認してみると、無事に起動していました。

c:\Vagrant\centos7p3&gt; vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run <code>vagrant halt</code> to
shut it down forcefully, or you can run <code>vagrant suspend</code> to simply
suspend the virtual machine. In either case, to restart it again,
simply run <code>vagrant up</code>.

仮想マシンの中身を見てみる

仮想マシンにログインして、Python3がインストールされているかを確認してみましょう。

c:\Vagrant\centos7p3&gt; vagrant ssh
Last login: Mon Oct  8 17:14:43 2018 from 192.168.33.1
Last login: Mon Oct  8 17:14:43 2018 from 192.168.33.1
[vagrant@localhost ~]$ python3 -V
Python 3.6.5
[vagrant@localhost ~]$

Python3がインストールされた状態になっていますね。

スポンサーリンク

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク