Vagrant で仮想マシンを構築する(1) Vagrant インストールから Vagrant Share での仮想マシン公開
Vagrant 1.5 以降に対応したインストール、設定メモです。
Vagrant 1.5 から Vagrant Cloud や Vagrant Share が使えるようになったので、そのあたりも書いていこうと思います。
ホストマシンは Mac OS X です。
環境構築(VirtualBox, Vagrant のインストール)
1. VirtualBox のインストール
-
- VirtualBox から最新バージョンをダウンロードして、インストールします
2. Vagrant のインストール
-
- Vagrant から最新バージョンをダウンロードして、インストールします
仮想マシンの起動と SSH ログイン
1. 作業用ディレクトリを作成して、vagrant init と vagrant up を実行します。今回は Ubuntu 14.04 LTS な仮想マシンを起動します。
$ mkdir -p vagrant_example $ cd vagrant_example $ vagrant init ubuntu/trusty64 $ vagrant up
$ vagrant ssh
仮想マシンの操作コマンド
- 仮想マシンのステータス確認
$ vagrant status
$ vagrant ssh
- 仮想マシンの停止/起動
$ vagrant halt $ vagrant up
$ vagrant suspend
$ vagrant up
- 仮想マシンの再起動
$ vagrant reload
- 仮想マシンの削除
$ vagrant destroy
仮想マシンのネットワークと共有フォルダ設定
以下の設定を追加します。
$ mkdir data $ vi Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 80, host: 8888 <- 追記 config.vm.synced_folder "./data", "/home/vagrant/data" <- 追記 end $ vagrant reload
Web サーバの起動
仮想マシンで Web サーバを起動します。
@local host $ cd vagrant_example/data $ vi index.html Hello, Vagrant! @virtual host $ cd /home/vagrant/data $ $ sudo python -m SimpleHTTPServer 80
ホストのブラウザで、http://localhost:8888/index.html へアクセスします。
Vagrant Share で Web サーバを公開
Vagrant 1.5 から使えるようになった Vagrant Share で Web サーバを公開します。
Vagrant Share とは
Vagrant Share allows you to share your Vagrant environment with anyone in the world
インターネットから仮想マシンへアクセスできる機能です。
Vagrant Share を使ってみる
Vagrant Share の使用には Vagrant Cloud へのアカウント作成が必要です。
1. Vagrant Cloud アカウント作成
- Atlas by HashiCorp で SIGN UP します。
2. vagrant login
$ vagrant login In a moment we will ask for your username and password to HashiCorp's Atlas. After authenticating, we will store an access token locally on disk. Your login details will be transmitted over a secure connection, and are never stored on disk locally. If you do not have an Atlas account, sign up at https://atlas.hashicorp.com. Atlas Username: <- Vagrant Cloud に登録したユーザ名 Password (will be hidden): <- Vagrant Cloud に登録したパスワード You are now logged in.
3. vagrant share
$ vagrant share ..... ==> default: Your Vagrant Share is running! Name: gigantic-blizzard-5997 ==> default: URL: http://gigantic-blizzard-5997.vagrantshare.com ==> default: ==> default: You're sharing your Vagrant machine in "restricted" mode. This ==> default: means that only the ports listed above will be accessible by ==> default: other users (either via the web URL or using `vagrant connect`).
vagrant share コマンド実行時に最後の方に表示される URL(上記の場合だと、http://gigantic-blizzard-5997.vagrantshare.com) にアクセスすると、仮想マシンで起動した Web サーバにアクセスできます。
URL は、vagrant share 実行毎に変わります。