Vagrant 介紹與基本操作
01. 虛擬化技術(Virtualization)介紹
1-1 虛擬化技術分類
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
1-2 虛擬機安裝方式分類
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
02. Vagrant 簡介
- Vagrant 由 HashiCorp 開發
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Terraform 也是由 HashiCorp 開發
- Terraform:infrastructure as code software which enables provisioning and adapting virtual infrastructure across all major cloud providers.
- The primary usage of Terraform is for managing remote resources in cloud providers such as AWS. Terraform is designed to be able to manage extremely large infrastructures that span multiple cloud providers. Vagrant is designed primarily for local development environments that use only a handful of virtual machines at most.
- Vagrant is for development environments. Terraform is for more general infrastructure management.
- 用於構建和管理虛擬機環境的工具。
- 提供易於配置(configure)、可重現(reproducible)和可攜性(portable)的工作環境。
- 支援 Shell 腳本、Chef 或 Puppet 等標準配置工具,可以在虛擬機上自動安裝和配置軟體。
Introduction to Vagrant
Vagrant vs. Terraform
03. Vagrant 運作方式及軟體安裝
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
04. Vagrant 基本操作介紹
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Git Hub Download
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
本次 Demo Vagrantfile
Virtualization (VT-x/AMD-V) - Enabling virtualization on your computer
Vagrant Cloud - Discover Vagrant Boxes
Official Boxes
- HashiCorp (the makers of Vagrant) publish a basic Ubuntu 18.04 64-bit box that is available for minimal use cases. It is highly optimized, small in size, and includes support for VirtualBox, Hyper-V, and VMware.
- For other users, we recommend the Bento boxes. The Bento boxes are open source and built for a number of providers including VMware, VirtualBox, and Parallels. There are a variety of operating systems and versions available.
4-1 從既有的 Vagrantfile 啟動虛擬機
- 切換至 .\docker-install 並執行
vagrant up
- row 18:配置 Private Key
- row 32:開始執行 shell 安裝 docker
- 驗證虛擬機狀態
vagrant status
4-2 進入虛擬機訪問 Guest OS
- 登入方式-01:
vagrant ssh
- 登入方式-02:
ssh -i
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
無法透過 ssh -i
登入處理方式
- 確認目錄 ~/.ssh 是否有先前產生的檔案 known_hosts ,刪除後就能成功透過密碼登入虛擬機
- 登入方式-03:
ssh -p [port] vagrant@127.0.0.1
調整 sshd_config,設定 PasswordAuthentication 的 Permission Policy
登入測試 Permission denied
4-3 暫停或重啟虛擬機
- 暫停虛擬機
vagrant suspend
- 重啟虛擬機
vagrant resume
4-4 停止並刪除虛擬機
加入 -f
選項強制刪除虛擬機 vagrant destroy -f
4-5 製作 Vagrantfile
透過 vagrant init
初始化一個新的 Vagrant 環境
本次所使用的 Vagrantfile
05. Docker 簡易操作
- 驗證 Docker 已經透過 Vagrantfile 在啟動虛擬機後,使用
inline: <<-SHELL
完成安裝
- 運行 Container
docker run
下載測試映像檔 (hello-world) 並在 Container 中運行,print 出訊息後退出(Exited) Container。
- 列出容器
- 運行中
docker ps
- 所有容器 (運行中-Up & 非運行中-Exited)
docker ps -a
- 安裝 NGINX 網頁伺服器
- 由於執行完 NGINX 會退出 Container,因此可以加入 Option
-d
在背景繼續執行
-p 80:80
將 Guest OS 的 80 port 路由到 Container 的 80 port
- 使用 localhost 的瀏覽器訪問 NGINX Container

Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
探討為何在 Localhost 要使用 8080 port 訪問 NGINX?
- 移除 NGINX Container 及 Image
- 列出運行中的 Container
docker ps
- 列出所有的 Container
docker ps -a
- 刪除所有的 Container 才能成功移除對應的 Image