写点什么

Windows 中使用 vagrant+virtual box 创建 Docker

用户头像
Java收录阁
关注
发布于: 2020 年 04 月 30 日

一、安装Vagrant

  1. 访问vagrant官网https://www.vagrantup.com/

  2. 下载对应系统的Vagrant

  3. 安装

  4. 命令行输入vagrant,测试是否安装成功

二、安装Virtual Box

  1. 访问virtualbox官网https://www.virtualbox.org/

  2. 选择左侧的downloads

  3. 选择下载对应的操作系统版本

  4. 安装

  5. [win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误

(1)打开服务

(2)找到Device Install Service和Device Setup Manager,然后启动

(3)再次尝试安装

三、安装Centos7

  1. 创建first-docker-centos7文件夹,并进入其中[目录路径中不要有中文字符]

  2. 在此目录下打开cmd,运行vagrant init centos/7 此时会在当前目录下生成Vagrantfile

  3. 运行vagrant up [注意不要运行,拉取远端的centos7太慢] 此时会找centos7的镜像,本地有就用本地的,本地没有就会拉取远端的

  4. 准备centos7的box

a. 下载centos box https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box

b. 将下载好的文件放到指定目录

c. 运行vagrant box add centos/7 D:\迅雷下载\virtualbox.box

d. vagrant box list 查看本地的box[这时候可以看到centos/7]

  1. 在first-docker-centos7文件夹下创建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

# 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"

# 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"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
vb.memory = "3000"
vb.name= "my-centos7"
vb.cpus= 2
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
  1. 根据本地的centos7 box创建虚拟机:

vagrant up[打开virtual box,可以发现centos7创建成功]

  1. vagrant基本操作

(1)vagrant ssh 进入刚才创建的centos7中

(2)vagrant status 查看centos7的状态

(3)vagrant halt 停止centos7

(4)vagrant destroy 删除centos7

(5)vagrant status 查看当前vagrant创建的虚拟机

(6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富 但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload

至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应配置



  1. 通过vagrant ssh可以进入虚拟机:



  1. 通过ip a命令查看当前虚拟机的IP地址:



  1. 修改root账号的password:

  1. vi /etc/ssh/sshd_config 修改PasswordAuthentication yes

  2. systemctl restart sshd

  3. 现在就可以通过root账号登陆虚拟机了



四、box的打包分发

  1. 退出虚拟机 vagrant halt

  2. 打包 vagrant package --output first-docker-centos7.box

  3. 得到first-docker-centos7.box

  4. 将first-docker-centos7.box添加到其他的vagrant环境中 vagrant box add first-docker-centos7 first-docker-centos7.box

  5. 得到Vagrantfile vagrant init first-docker-centos7

  6. 根据Vagrantfile启动虚拟机 vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]



五、安装docker

  1. 进入centos7 虚拟机

  2. 卸载之前的docker

sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine





  1. 安装必要的依赖

sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2



  1. 设置docker仓库

sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo



  1. 添加阿里云镜像https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ns2jtf98.mirror.aliyuncs.com"]
}
EOF



  1. 安装docker

sudo yum install -y docker-ce docker-ce-cli containerd.io



  1. 启动docker

sudo systemctl start docker && sudo systemctl enable docker



  1. 测试docker是否安装成功

sudo docker run hello-world



发布于: 2020 年 04 月 30 日阅读数: 121
用户头像

Java收录阁

关注

士不可以不弘毅,任重而道远 2020.04.30 加入

喜欢收集整理Java相关技术文档的程序员,欢迎关注同名微信公众号 Java收录 阁获取更多文章

评论

发布
暂无评论
Windows中使用vagrant+virtual box创建Docker