Vagrant文档地址 https://www.vagrantup.com/docs
1、下载安装VirtualBox、Vagrant
- 下载并安装VirtualBox https://www.virtualbox.org/wiki/Downloads
- 下载并安装Vagrant https://www.vagrantup.com/downloads.html
2、执行 vagrant version 命令查看Vagrant是否安装成功
vagrant version
3、初始化项目目录,创建Vagrantfile文件
- 新建一个文件夹,在文件夹下再创建两个文件夹vagrant和share_data(与Vagrantfile中的本地共享文件夹名称一致),在vagrant文件夹中创建Vagrantfile文件(注意没有后缀)
- Vagrantfile中安装了docker、git、JavaJDK、maven等,根据自己的需要选择
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 18080, host: 18080
config.vm.network "forwarded_port", guest: 18081, host: 18081
config.vm.network "forwarded_port", guest: 18082, host: 18082
config.vm.network "forwarded_port", guest: 18083, host: 18083
config.vm.network "forwarded_port", guest: 18084, host: 18084
config.vm.network "forwarded_port", guest: 18085, host: 18085
config.vm.network "forwarded_port", guest: 18086, host: 18086
config.vm.network "forwarded_port", guest: 18087, host: 18087
config.vm.network "forwarded_port", guest: 18088, host: 18088
config.vm.network "forwarded_port", guest: 18089, host: 18089
config.vm.network "forwarded_port", guest: 28081, host: 28081
config.vm.network "forwarded_port", guest: 28082, host: 28082
config.vm.network "forwarded_port", guest: 28083, host: 28083
config.vm.network "private_network", ip: "192.168.33.224"
config.vm.synced_folder '.', '/vagrant', disabled: true
config.vm.synced_folder "E:/git", "/git-resp", type: "nfs"
config.vm.provider "virtualbox" do |v|
v.memory = 4096
v.cpus = 2
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
config.vm.provision "shell", inline: <<-SHELL
sudo yum -y update
sudo timedatectl set-timezone "Asia/Shanghai"
sudo yum -y install vim
sudo yum -y install dos2unix
sudo yum -y install unzip zip
sudo yum -y install git
sudo yum -y install java-1.8.0-openjdk.x86_64
sudo yum -y install maven
sudo curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
sudo systemctl enable docker
sudo mkdir -p /etc/docker
sudo systemctl daemon-reload
sudo systemctl start docker
sudo docker volume create portainer_data
sudo docker run -d -p 18000:8000 -p 19000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
sudo curl -L https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
SHELL
end
4、安装WinNFSd插件,在Windows上管理并添加对NFS的支持。
- vagrant-winnfsd: https://github.com/winnfsd/vagrant-winnfsd
vagrant plugin install vagrant-winnfsd
vagrant plugin install vagrant-vbguest
5、Vagrant启动
- 在vagrant文件夹下,打开命令行,执行vagrant up 命令启动镜像,启动成功后,在文件夹下会出现一个.vagrant文件夹。
vagrant up #启动vagrant
- 使用vagrant ssh 命令连接虚拟机,默认账号 vagrant, 密码 vagrant
vagrant ssh #使用ssh