มาใช้ Vagrant กันเถอะ? ใช้งานยังไง? ลงยังไงมาลองกัน

Vagrant / 31 สิงหาคม 2020 / 270

คำสั่งหลักๆของ Vagrant
vagrant init ตามด้วยชื่อ image box
ตัวอย่าง vagrant init ubuntu/trusty64
คือคำสั่งสร้าง Config file สำหรับใช้งาน Box นั้นๆ จะมีชื่อไฟล์ว่า Vagrantfile

หาก Vagrant ไฟล์ที่ระบบสร้างยากต่อการเข้าใจ สามารถสร้างเอกได้น่ะ ใช้ได้หมด แค่เปลี่ยน Box ที่ต้องการใช้งานเท่านั้น ตัวอย่าง Vagrantfile ที่สามารถทำเองได้

Vagrant.configure(2) do |config|
config.vm.box = “ubuntu/trusty64”
config.vm.define :cyber do |cyber_config|
cyber_config.vm.host_name = “cyber”
cyber_config.vm.network “private_network”, ip: “192.168.5.200”
cyber_config.vm.network “private_network”, ip:”192.168.7.151″
cyber_config.vm.provider :virtualbox do |vb|
vb.customize [“modifyvm”, :id, “–memory”, “512”]
vb.customize [“modifyvm”, :id, “–cpus”, “2”]
end
end

end

Download Vagrantfile ได้ที่นี่ : https://drive.google.com/file/d/1wF45I6WVau8p-0SR_8KU76OEsxlU47-A/view?usp=sharing

สำหรับท่านที่ต้องการ Build VM ที่เป็น Windows ให้เพิ่มคำสั่งนี้ vb.gui = true เพื่อให้ GUI สามารถทำงานได้ โดยจะขึ้นหน้า Remote GUI แทน SSH

vagrant box add ตามด้วยชื่อ image box
ตัวอย่าง vagrant box add ubuntu/trusty64
คือคำสั่งสร้าง Download Image box ที่ต้องการใช้งาน มาไว้ที่เครื่องที่จะรัน VM

vagrant box list
คือคำสั่งแสดง Download box list เป้นคำสั่งเรียกดู box image ทั้งหมดในเครื่อง

vagrant up ตามด้วยชื่อ computer name
ตัวอย่าง : vagran up dn03
คือคำสั่ง build vm ใน Virtual box เพื่อใช้งาน

vagrant ssh ตามด้วยชื่อ computer name
ตัวอย่าง : vagrant ssh dn03
คือคำสั่งเข้าใช้งาน VM ผ่าน ssh
จากรูปการเข้าใช้งานสิทธิ์ root ใช้คำสั่ง su -l
ส่วน password default คือ vagrant

vagrant halt ตามด้วยชื่อ computer name
ตัวอย่าง : vagrant halt dn03
คือคำสั่ง shutdown vm

vagrant destroy ตามด้วยชื่อ computer name
ตัวอย่าง : vagrant destroy dn03
คือ คำสั่ง Remove VM ออกจาก Virtual box


สำหรับท่านที่ต้องการหา BOX image สามารถหาได้ที่ลิงค์นี้
https://app.vagrantup.com/boxes/search

สามารถดูชื่อ Image box เพื่อไปใส่ vagrantfile ได้ที่นี่

Comment