Engineering

Mar 05, 2017

Vagrant コマンドまとめ(前半戦) | vagrant command line interface

 

ある日vagrantコマンドを引数なしで 叩いてしまったことが ありました。

 

そうするとvagrantコマンドの一覧が出てきました。 コマンドではままあることなので、 驚きはしなかったのですが、 知らないコマンドがままあったので、 調べてまとめてみました。

と思いましたが、 途中で息切れしたので、前半戦だけ。

 

box             manages boxes: installation, removal, etc.
connect         connect to a remotely shared Vagrant environment
destroy         stops and deletes all traces of the vagrant machine
global-status   outputs status Vagrant environments for this user
halt            stops the vagrant machine
help            shows the help for a subcommand
init            initializes a new Vagrant environment by creating a Vagrantfile
login           log in to HashiCorp's Atlas
package         packages a running vagrant environment into a box
plugin          manages plugins: install, uninstall, update, etc.
port            displays information about guest port mappings
powershell      connects to machine via powershell remoting
provision       provisions the vagrant machine
push            deploys code in this environment to a configured destination
rdp             connects to machine via RDP
reload          restarts vagrant machine, loads new Vagrantfile configuration
resume          resume a suspended vagrant machine
share           share your Vagrant environment with anyone in the world
snapshot        manages snapshots: saving, restoring, etc.
ssh             connects to machine via SSH
ssh-config      outputs OpenSSH valid configuration to connect to the machine
status          outputs status of the vagrant machine
suspend         suspends the machine
up              starts and provisions the vagrant environment
version         prints current and latest Vagrant version

全部で25個です。

 

1. vagrant up

まずは簡単なところから, vagrantのイメージを起動します。 さらに起動後にprovisionも実行してくれます。

 

2. vagrant halt

イメージの停止。

 

3. vagrant reload

イメージの再起動。

 

4. vagrant version

vagrantのバージョンを標準出力に出力。

$vagrant version
Installed Version: 1.9.1
Latest Version: 1.9.2

To upgrade to the latest version, visit the downloads page and
download and install the latest version of Vagrant from the URL
below:

  https://www.vagrantup.com/downloads.html

If you're curious what changed in the latest release, view the
CHANGELOG below:

  https://github.com/mitchellh/vagrant/blob/v1.9.2/CHANGELOG.md

 

 

5. vagrant status

vagrantイメージの状態(起動/停止)を表示。

6. vagrant destroy

イメージの削除。 こうやってサーバを簡単に作ったり、消したり できるのが良いところですよね。

 

7. vagrant init

vagrant の初期化。 vagrant init で初期化ファイルを作成してくれる。 ubuntu 16を使いたい時は、

vagrant init ubuntu/xenial64

第一引数にをconfig.vm.boxに設定してVagrantfile を作成してくれる。

--minimalオプションをつけると 最小限の記述のVagrantファイルを作成してくれる。

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

urlからboxを作成する場合は、

$ vagrant init my-company-box https://boxes.company.com/my-company.box

のようにする。

8. vagrant ssh

ゲストOSにssh接続。 -cオプションをつけると,ssh接続してコマンドを投げられる。

9. vagrant ssh-config

vagrant sshする時のsshの設定を出力。

$vagrant ssh-config
Host default
  HostName 127.0.0.1
  User ubuntu
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/jo/Vagrant/node/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

10. vagrant suspend

イメージの一時停止。

 

11. vagrant resume

一時停止されたイメージの再起動。

 

12. vagrant box

vagrant boxの管理。 box とは、vagrantイメージにインストールする際の OSのスナップショットのようなもの。

  • list ・・・ Vagrant Box の表示。
  • add ・・・ Vagrant Box の追加。
  • remove ・・・ Vagrant Box の削除。
  • outdated ・・・ Vagrant Box が最新かどうか確認。
  • update ・・・ Vagrant Boxを最新にアップデート。
  • repackage ・・・ 指定したイメージをboxにパッケージする。
 

13. vagrant global-status

ホストマシーンのvagrantイメージの状態を確認できます。 runningなbox2を停止させたい場合は、 vagrant halt a8f6bbcで停止できる。

vagrant global-status
id       name    provider   state    directory
-------------------------------------------------------------------------
4a9f8c4  default virtualbox poweroff /Users/version1/Vagrant/box1
a8f6bbc  default virtualbox running  /Users/version1/Vagrant/box2
a08e8e9  default virtualbox running  /Users/version1/Vagrant/box3

 

14. vagrant port

こんな感じで、 ゲストマシーンのホストマシーンにマッピングされた ポートを確認できる。

$vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2222 (host)

 

15. vagrant powershell

powershellを起動できる模様。 macでやって見たけど、普通にwindowsじゃないとねと言われる。

 

全部まとめようとしましたが、 息切れしてきたので、 前半として一旦区切ります。

調子の良い時に後半戦まとめます。

関連記事

記事検索

気になるサイト内の記事を検索する

プロフィール

バンクーバー在住のフルスタックエンジニアです。React, Ruby on Rails, Go などでお仕事しています。職場がトロントなので日本、トロント、バンクーバーの三つの時天空を操って生活しています。

プロモーション