Win7通过Vagrant管理docker开发环境

Vagrant 环境和docker 容器不管是对于本地团队开发带来方便,  线上部署docker集群为应用提供微服务也是目前比较流行的一种趋势, 这里记录下本地部署的安装过程;

1. 官网下载win7版本:

virtualbox: https://www.virtualbox.org/wiki/Downloads
vagrant: https://www.vagrantup.com/downloads.html
docker box: 不用自己做base box,直接用别人做好的,同时先下载到本地更好:

1). https://atlas.hashicorp.com/boxes/search 或
2). http://www.vagrantbox.es/ 比如找到ubunut官方提供的14.04: https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

2. 安装:

安装vbox和vagrant 后重启电脑;

3. 添加box:

$ cmd
$ d:
$ cd Vagrant
$ mkdir root
$ cd root
$ vagrant box add mybox file:///d:/software/trusty-server-cloudimg-amd64-vagrant-disk1.box
若vagrant没有加入到系统变量path,则需要先cd到Vagrant/bin执行;

==> box: Box file was not detected as metadata. Adding it directly…
==> box: Adding box ‘mybox’ (v0) for provider:
box: Unpacking necessary files from: file:///d:/software/trusty-server-cloudimg-amd64-vagrant-disk1.box
box: Progress: 100% (Rate: 20.0M/s, Estimated time remaining: –:–:–)
==> box: Successfully added box ‘mybox’ (v0) for ‘virtualbox’!

$ vagrant init mybox 在当前目录(可以是web根目录)生成Vagrantfile文件(注意若没有指定mybox默认init 的是base box)
$ vagrant up 会一直提示使用hyper-v这个provider:
将virtualbox的执行路径加入系统变量的path中,然后重启电脑;
再次执行vagrant up:

default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default: Guest Additions Version: 4.3.10
default: VirtualBox Version: 5.0==>
default: Mounting shared folders…
default: /vagrant => D:/Vagrant/root

因为我们下载的是最新的virtualbox, 而ubuntu 的box 还是4.3的版本,需要升级;或者重新下载vbox的相应版本。

$ vagrant ssh 提示不可执行:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: D:/Vagrant/root/.vagrant/machines/default/virtualbox/private_key

下载putty ssh客户端登录用户名密码都是vagrant,当然也可以用key(先Load Session,然后Conection设置data中的自动登录用户名,SSH Auth中设置privacy_key,然后在Session中点击保存),但使用上面的privacy_key文件出现错误提示:

Unable to use key file “D:\Vagrant\root\.vagrant\machines\default\virtualbox\private_key” (OpenSSH SSH-2 private key) Using username “vagrant”.

需要使用PUTTYGEN.EXE通过File菜单导入上面的privatekey转换成putty认识的格式,若有不清楚请看详情:
http://www.alittleofboth.com/2014/04/putty-unable-to-use-vagrants-private-key/

4. 退出vagrant:

vagrant suspend 保存客户机状态退出
vagrant halt 正常关闭客户机,一般用这个关主机时
vagrant destroy 清除所有客户机磁盘内容
以上命令使用vagrant up启动客户虚拟机所需时间依次增长;

5. 关于Vagrantfile的配置:

默认主机的vagrant根目录D:/Vagrant/root和客户机的/vagrant会自动同步,可以自己定制:
config.vm.synced_folder “../data”, “/data”

执行脚本等自动化:
config.vm.provision :shell, path: “bootstrap.sh”
bootstrap.sh放在vagrant根目录下:

#!/usr/bin/env bash

sudo apt-get update
sudo apt-get install -y apache2
if ! [ -L /var/www ]; then
sudo rm -rf /var/www
sudo ln -fs /data /var/www
fi

vagrant provision (只有第一次up时会执行,因此后面需要执行该命令执行)

主机和客户机的端口映射:
config.vm.network :forwarded_port, guest: 80, host: 80
(主机完全在虚拟机中开发,会提示管理员用户运行才可以设置小于1024的端口,如果80有问题则要设置大于1024)

现在测试下,在本地主机D:/Vagrant/data目录下新建 html/index.html (映射到客户机的/var/www/html即Apache的默认web目录):

<h1>Hello Vagrant</h1>

本地主机浏览器打开应该就可以看到效果了:http://localhost/

6. putty ssh进入ubuntu客户机中安装docker:

curl -sSL https://get.docker.com/ | sh
如没有安装curl需提前安装: sudo apt-get install curl
检查是否安装成功: sudo docker run hello-world

7. windows 开启关闭bat脚本:

::Vagrant Manager
::------------------------------------------------------------------------------------
@echo off
mode con cols=80 lines=35
title Vagrant启动和关闭
color 2f
set fgx=--------------------------------------------------------------------------------
cls
call :about
echo.
echo %fgx%
echo 提示:此 bat 工具需要以管理员权限运行(在此 bat 文件上右击选择“以管理员身份运行”)
echo.
goto cmdfinish

:cmdlist
cls
echo ++++++++++++++++++++++++++++++++主菜单++++++++++++++++++++++++++++++++
echo 1 启动关闭
echo [1] 启动
echo [2] 关闭
echo [9] 关于
echo [0] 退出
echo.
set /a choice= 9999
set /p choice=请选择功能(输入序号):
echo %fgx%
if %choice%==0 exit
if %choice%==1 call :cmd1 & goto cmdfinish
if %choice%==2 call :cmd2 & goto cmdfinish
if %choice%==9 call :about & goto cmdfinish
::goto cmdlist
exit

:cmdfinish
echo %fgx%
::pause
goto cmdlist

:cmd1
echo 启动中...
cd /d d:\vagrant\root
vagrant up
echo %fgx%
::goto :eof
exit

:cmd2
echo 关闭中...
cd /d d:\vagrant\root
vagrant halt
echo %fgx%
::goto :eof
exit

:about
echo.
echo ┏━━━━━━━━━━━ Vagrant Up and Halt ━━━━━━━━━━┓
echo ┃ ┃
echo ┃ * 适用于 Windows 7 ┃
echo ┃ * 作者: Harry ┃
echo ┃ ┃
echo ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
echo [2015 - 08 - 20]
echo.
goto :eof
exit

exit

Leave a Reply

Your email address will not be published. Required fields are marked *