我的需求
在 mac 和 linux 上 docker 是以虛擬 linux 網路模式運作的,這就導致我們沒有辦法直接訪問容器,在電腦本身有環境的情況下不去影響當前環境搭建其他環境(其他語言環境可以按照這個思路來)
準備工作
- virtualbox 或 vmware 任選其一
- vagrantup 必備
為什麼使用 vagrantup
我們通常在配置虛擬機器的時候需要自己按照系統配置宿主機和虛擬機器的網路等使用 vagrantup 能讓我們快速並且簡單的執行
注意
vagrantup 映象下載非常慢,可以使用 外網加速 的方式
vagrantup 似乎已經上了CDN可以等待一會速度會上去的
終端可以使用類似命令 或者 全域性
export http_proxy="http://127.0.0.1:20050"
export https_proxy="http://127.0.0.1:20050"
開始搭建
在安裝好上述軟體之後,開啟命令列或終端
使用 vagrant up
檢視是否安裝成功,會輸出版本號
新建環境目錄
用處:存放需要掛載到虛擬機器的資料夾或檔案
/Users/cq/codeRunTime $: ls -al
drwxr-xr-x 4 chenquan staff 128 4 24 22:28 .vagrant
-rw-r--r--@ 1 chenquan staff 1203 4 26 14:29 Vagrantfile
drwxr-xr-x 5 chenquan staff 160 4 26 11:12 backup
drwxr-xr-x 9 chenquan staff 288 4 28 14:56 data
drwxr-xr-x 5 chenquan staff 160 4 26 01:28 wwwlogs
drwxr-xr-x 6 chenquan staff 192 4 28 15:01 wwwroot
注意我這裡是使用了寶塔所以會出現 backup,data,wwwlogs,wwwroot 等目錄
在當前目錄初始化一個虛擬機器
我是用的映象是 centos/7 有其他需求的同志可以在 vagrantup search 尋找自己想要的
vagrant init centos/7 # 在當前目錄初始化,目錄下會多一個名為 Vagrantfile 的檔案
vagrant up # 啟動當前目錄下的映象
正常輸出 vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
正常輸出 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'... # 這一步會下載映象檔案,由於我本地已經存在可以直接使用
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Setting the name of the VM: ccc_default_1556587395244_89525
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2200
default: SSH username: vagrant
default: SSH auth method: private key # 這裡你可能會等待很久不用擔心
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
配置固定 IP
上一步在目錄下會生成 Vagrantfile 檔案,我們開啟它
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos/7" # 映象名稱
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false # 啟動是否檢查映象更新?,更新會導致之前映象配置或者按照好的環境丟失!!!!
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080 # 轉發埠暫時不需要用到,如果你不想工作在獨立的IP上可以配置此項繫結到 127.0.0.1 上
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10" # 配置虛擬機器的私有網路,這裡就是關鍵處
config.vm.network "private_network", ip: "192.101.101.101" # 記住這裡設定的IP不要與當前網路環境有衝突!!
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data" # 掛載目錄,這裡看個人需求,如果可能經常變動你的虛擬機器環境你可以只掛載你的程式碼目錄上去
config.vm.synced_folder "../data", "/vagrant_data" # 掛載當前系統的 ../data 資料夾 到 虛擬機器的 /vagrant_data ,當然還有其他引數 owner:"www", group:"www" 指定使用者組和使用者 create:true 是否啟用
# 我的完整配置如下
# config.vm.synced_folder "/Users/cq/codeRunTime/wwwroot", "/www/wwwroot" , create:true , owner:"www", group:"www"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
# 這裡可以開始對虛擬的一些其他配置,比如 vb.gui 是否顯示 主機的介面,memory 記憶體 ,cpus 這些大家都可以在vargent up的文件中找到
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# 這裡可以讓你在啟動虛擬機器後自動執行某些命令
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
在編輯好你的配置檔案後使用 vagrant reload
進行過載配置並啟動虛擬機器
可能出現的問題
-
Q: 映象下載失敗,無法初始化虛擬機器
A: 使用科學上網或者手動下載虛擬機器的檔案(路徑在你執行
vargent up
下載的那個就是) 手動下載然後使用vagrant box add
命令進行手動匯入 -
Q: 虛擬機器IP無法訪問
A: 在重新配置 IP 在 virtualbox 或 vm 的管理軟體中將網路卡刪除重新使用
vagrant reload
啟動虛擬機器 -
Q: 一切正常後,我想泛解析測試域名到虛擬機器上如 *.ddt.test => 192.101.101.101
A: 使用 dnsmaq 或者其他的相關軟體,當自己的dns解析改為 127.0.0.1 最後在 dnsmaq 的配置檔案加入
address=/ddt.test/192.101.101.101
我的 dnsmaq 配置檔案resolv-file=/usr/local/etc/resolv.dnsmasq.conf # 配置前置dns用來解析 www.baidu.com 等網站 strict-order # 按照上述檔案的順序一次解析,解析不到在用下方address配置 listen-address=127.0.0.1 # 監聽IP conf-dir=/usr/local/etc/dnsmasq.d # dnsmaq配置檔案 這是自帶的不需要更改! cache-size=10000 # 快取大小 address=/ddt.test/192.101.101.101 # 配置域名到IP address=/think.test/192.101.101.101 address=/laravel.test/192.101.101.101 address=/php.test/192.101.101.101
-
Q: 每次都需要到目錄去執行
vagrant up
等命令很麻煩,如何簡化?A: 可用方法很多,Windows設定環境變數,類 linux 系統配置 bash_profile 這裡貼出我的配置
alias phper="cd /Users/cq/codeRunTime && vagrant" # 配置phper 啟動虛擬機器在任意地方我可以使用 phper up 等來快速操作 alias phperdns-re="sudo brew services restart dnsmasq" # 重啟dns服務 alias phperdns-conf="open /usr/local/etc/dnsmasq.conf" # 開啟dns配置檔案 alias phperopen="export http_proxy='http://127.0.0.1:20050' && export https_proxy='http://127.0.0.1:20050'" #設定網路加速
未完待續...
本作品採用《CC 協議》,轉載必須註明作者和本文連結