本文簡(jiǎn)單介紹如何在Windows系統(tǒng)中使用vagrant+virtual box創(chuàng)建Docker環(huán)境 一、安裝Vagrant1. 訪問vagrant官網(wǎng)Vagrant 2. 下載對(duì)應(yīng)系統(tǒng)的Vagrant 3. 安裝 4. 命令行輸入vagrant,,測(cè)試是否安裝成功
二,、安裝Virtual Box 1. 訪問virtualbox官網(wǎng)Virtual Box 2. 選擇左側(cè)的downloads 3. 選擇下載對(duì)應(yīng)的操作系統(tǒng)版本 4. 安裝 5. [win10中若出現(xiàn)]安裝virtualbox快完成時(shí)立即回滾,并提示安裝出現(xiàn)嚴(yán)重錯(cuò)誤 (1)打開服務(wù) (2)找到Device Install Service和Device Setup Manager,,然后啟動(dòng) (3)再次嘗試安裝 三,、安裝Centos7 1. 創(chuàng)建first-docker-centos7文件夾,并進(jìn)入其中[目錄路徑中不要有中文字符] 2. 在此目錄下打開cmd,,運(yùn)行vagrant init centos/7 此時(shí)會(huì)在當(dāng)前目錄下生成Vagrantfile 3. 運(yùn)行vagrant up [注意不要運(yùn)行,,拉取遠(yuǎn)端的centos7太慢] 此時(shí)會(huì)找centos7的鏡像,本地有就用本地的,,本地沒有就會(huì)拉取遠(yuǎn)端的 4. 準(zhǔn)備centos7的box a. 下載centos box Centos7 b. 將下載好的文件放到指定目錄 c. 運(yùn)行vagrant box add centos/7 D:\迅雷下載\virtualbox.box d. vagrant box list 查看本地的box[這時(shí)候可以看到centos/7] 5. 在first-docker-centos7文件夾下創(chuàng)建Vagrantfile文件,,文件內(nèi)容如下: # -*- 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..
# Every Vagrant development environment requires a box. You can search for # boxes at https:///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
6. 根據(jù)本地的centos7 box創(chuàng)建虛擬機(jī): vagrant up[打開virtual box,可以發(fā)現(xiàn)centos7創(chuàng)建成功] 7. vagrant基本操作 (1)vagrant ssh 進(jìn)入剛才創(chuàng)建的centos7中 (2)vagrant status 查看centos7的狀態(tài) (3)vagrant halt 停止centos7 (4)vagrant destroy 刪除centos7 (5)vagrant status 查看當(dāng)前vagrant創(chuàng)建的虛擬機(jī) (6)Vagrantfile中也可以寫腳本命令,,使得centos7更加豐富 但是要注意,,修改了Vagrantfile,要想使正常運(yùn)行的centos7生效,,必須使用vagrant reload 至此,,使用vagrant+virtualbox搭建centos7完成,,后面可以修改Vagrantfile對(duì)虛擬機(jī)進(jìn)行相應(yīng)配置 8. 通過vagrant ssh可以進(jìn)入虛擬機(jī): 9. 通過ip a命令查看當(dāng)前虛擬機(jī)的IP地址: 10. 修改root賬號(hào)的password: 11. vi /etc/ssh/sshd_config 修改PasswordAuthentication yes 12. systemctl restart sshd 13. 現(xiàn)在就可以通過root賬號(hào)登陸虛擬機(jī)了 四、box的打包分發(fā)1. 退出虛擬機(jī) vagrant halt 2. 打包 vagrant package --output first-docker-centos7.box 3. 得到first-docker-centos7.box 4. 將first-docker-centos7.box添加到其他的vagrant環(huán)境中 vagrant box add first-docker-centos7 first-docker-centos7.box 5. 得到Vagrantfile vagrant init first-docker-centos7 6. 根據(jù)Vagrantfile啟動(dòng)虛擬機(jī) vagrant up [此時(shí)可以得到和之前一模一樣的環(huán)境,,但是網(wǎng)絡(luò)要重新配置] 五,、安裝docker1. 進(jìn)入centos7 虛擬機(jī) 2. 卸載之前的docker sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
3. 安裝必要的依賴 sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
4. 設(shè)置docker倉庫 sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
5. 添加阿里云鏡像阿里云鏡像 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://ns2jtf98.mirror."] } EOF
6. 安裝docker sudo yum install -y docker-ce docker-ce-cli containerd.io
7. 啟動(dòng)docker sudo systemctl start docker && sudo systemctl enable docker
8. 測(cè)試docker是否安裝成功 sudo docker run hello-world
|