LoginSignup
1
4

More than 5 years have passed since last update.

WindowsでVagrantを使ってみる.(ログ)

Last updated at Posted at 2018-03-30

Macとかでの解説が多いので,忘れたときに戻ってくる備忘録的なもの

VagrantとVirtualboxのインストールは終わったところからスタート.

  • まずはコマンドプロンプトを開く.(ここではanaconda promptを用いた)
  • バージョンの確認

vagrant --version

(base) C:\Users\User>vagrant --version
Vagrant 2.0.3

プロキシの設定

もし,会社のインターネット等でプロキシ越しにダウンロードしたい場合のみ.

set http_proxy=<ホストアドレス>:<ポート番号>
set https_proxy=<ホストアドレス>:<ポート番号>

なお,この設定はコマンドプロンプトを閉じると消えるので,開くたびに毎回設定すべし.
またはシステムの詳細設定から環境変数をいじるべし.

https://weblabo.oscasierra.net/windows-vargent-proxy/

boxのダウンロード

  • 練習用のboxをダウンロード

vagrant box add precise32 http://files.vagrantup.com/precise32.box

(base) D:\hnishi\dotinstall\lesson_vagrant>vagrant box add precise32 http://files.vagrantup.com/precise32.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'precise32' (v0) for provider:
    box: Downloading: http://files.vagrantup.com/precise32.box
    box: Download redirected to host: hashicorp-files.hashicorp.com
    box: Progress: 59% (Rate: 224k/s, Estimated time remaining: 0:09:35))

結構時間かかっております.

  • 取得したboxのリストを表示

vagrant box list

(base) D:\hnishi\dotinstall\lesson_vagrant>vagrant box list
precise32 (virtualbox, 0)
  • 他のboxの取得

↓のサイトに色々なboxが置いてある.
http://www.vagrantbox.es/

centos64ビットのboxを取得してみる.

コピーして

image.png

boxをaddする

vagrant box add centos64 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box

(base) D:\hnishi\dotinstall\lesson_vagrant>vagrant box add centos64 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos64' (v0) for provider:
    box: Downloading: https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box
    box: Download redirected to host: github-production-release-asset-2e65be.s3.amazonaws.com
    box: Progress: 2% (Rate: 319k/s, Estimated time remaining: 0:36:42)4)

確認する

(base) D:\hnishi\dotinstall\lesson_vagrant>vagrant box list
centos64  (virtualbox, 0)
precise32 (virtualbox, 0)

ヘルプの出し方は,

vagrant box -h

boxを削除するときは,

vagrant box remove <boxの名前>

boxは,デフォルトで以下のようなディレクトリに格納される.

(base) D:\hnishi\dotinstall\lesson_vagrant>dir C:.vagrant.d\boxes
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は 15C2-5150 です

 C:\Users\User\.vagrant.d\boxes のディレクトリ

2018/03/28  17:50    <DIR>          .
2018/03/28  17:50    <DIR>          ..
2018/03/28  17:50    <DIR>          centos64
2018/03/28  17:06    <DIR>          precise32
               0 個のファイル                   0 バイト
               4 個のディレクトリ  190,804,291,584 バイトの空き領域

仮想マシンを立ち上げる

mkdir myCentOSVM
cd myCentOSVM
vagrant init centos64
more Vagrantfile
  • 仮想マシンの起動

vagrant up

仮想マシンの制御

  • 仮想マシンの状態確認

vagrant status

  • 仮想マシンの一時休止

vagrant suspend

  • 仮想マシンの一時休止の再開

vagrant resume

  • 仮想マシンのシャットダウン

vagrant halt

  • 仮想マシンの再起動

vagrant reload

  • 仮想マシンの削除

vagrant destroy

仮想マシン側のProxyを設定する

参考 --> http://maku77.github.io/vagrant/proxy.html

vagrant plugin install vagrant-proxyconf

Vagrantfileに以下の2-4行目を追記

Vagrant.configure("2") do |config|
  config.proxy.http     = "http://<host address>:8080"
  config.proxy.https    = "http://<host address>:8080"
  config.proxy.no_proxy = "localhost,127.0.0.1"
end

vagrant up

仮想マシンにssh接続で入る

vagrant ssh

(base) D:\hnishi\dotinstall\lesson_vagrant\myCentOSVM>vagrant ssh
Last login: Wed Mar 28 11:29:30 2018 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.

httpサーバー環境の構築

  • sshで入った仮想マシンの中で以下を打つ.
sudo yum -y install httpd
sudo service httpd start
sudo chkconfig httpd on
sudo service iptables stop
sudo chkconfig iptables off
  • viエディタで /var/www/html/index.html を編集.

sudo vi /var/www/html/index.html

index.html
Hello World!
  • exit で抜けて,Vagrantfileをテキストエディタで編集する,
  #config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.network "private_network", ip: "192.168.33.10"

のように,#(コメントアウト)を外す.

  • 仮想マシンの再起動.

vagrant reload

この後,ブラウザのURLに
192.168.33.10を打てば,Hello World! と表示される...はずなのだが,表示されなくてタイムアウトになった.おそらくネットワークアダプタとかプロキシの問題.
プロキシを使わない場合にはすんなりいくはず(未検証).
たしか,前にも同じような問題になってポートフォワードとかいじってうまくいったけど,あまり重要でないので,次に進む.

共有フォルダ

Vagrantfileがある D:\hnishi\dotinstall\lesson_vagrant\myCentOSVM のフォルダの中の内容は,仮想マシンの中の /vagrant にマウントされている.

たとえば, vagrant ssh で入って,

[vagrant@localhost ~]$ cd /vagrant
[vagrant@localhost vagrant]$ ls
Vagrantfile  

というふうに,確認できる.

さきほど作成したwebページのindex.htmlをホスト側で編集してindex.htmlに反映させたい場合は,

仮想マシンの中で

sudo rm -rf /var/www/html
sudo ln -s /vagrant /var/www/html
sudo echo "hello world from win10" > /vagrant/index.html

というようにシンボリックリンクを貼ればよい.

参考

1
4
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
1
4