2.Vagrant搭建统一开发环境 docker环境


前言: 🌰安装docker开发环境

1.准备工作,ubuntu基础操作
2.安装docker开发环境
3.环境打包、分发
4.测试Box

1.准备工作,ubuntu基础操作

1.1 添加box

➜ vagrant add box ubuntu/trusty64

1.2 创建ubuntu目录,并初始化ubuntu box

➜ vagrant init ubuntu/trusty64

1.3 登录虚拟机

➜ vagrant ssh

1.4 替换ubuntu源

# 4.1备份源
➜ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
#4.2 替换源为下面内容
➜ sudo vim /etc/apt/sources.list

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

# 4.3更新源
➜ sudo apt-get update

2.安装docker开发环境

2.1 安装docker

# 安装
➜ sudo apt-get install docker-ce
# 运行docker
➜ sudo service docker start

2.2 安装docker-compose

# 下载docker-compose
➜ sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

# 添加权限
➜ sudo chmod +x /usr/local/bin/docker-compose

# demo
vagrant@vagrant-ubuntu-trusty-64:~$ docker -v
Docker version 18.06.1-ce, build e68fc7a
vagrant@vagrant-ubuntu-trusty-64:~$ docker-compose -v
docker-compose version 1.17.0, build ac53b73

3.环境打包、分发

3.1 关闭虚拟机

# 退出虚拟机
➜  exit
# 关闭虚拟机
➜  vagrant halt

3.2 打包

# 打包
➜  ubuntu vagrant package --output ubuntu-docker.box

4.测试Box

4.1 添加box

➜  ubuntu vagrant box add ubuntu-docker ./ubuntu-docker.box

4.2 创建VM目录,并初始化

➜  mkdir ubuntu-docker1 && cd ubuntu-docker1 && vagrant init ubuntu-docker

4.3 编辑Vagrantfile文件,添加私有网络

 ...
config.vm.box = "ubuntu-docker"
config.vm.network "private_network", ip: "192.168.199.222"
...

4.4 登录VM测试

➜  vagrant up
➜  vagrant ssh

总结

将打包好的box分享即可统一环境

配置vagrant网络设置
config.vm.network "private_network", ip: "192.168.199.101"

推荐阅读更多精彩内容