docker安裝(在Win10上準備centos7 採用vagrant+virtual box)
#### 1.1 下載安裝vagrant
01 訪問Vagrant官網
https://www.vagrantup.com/
02 點選Download
Windows,MacOS,Linux等
03 選擇對應的版本
04 傻瓜式安裝
05 命令列輸入vagrant,測試是否安裝成功
#### 1.2 下載安裝virtual box
01 訪問VirtualBox官網
https://www.virtualbox.org/
02 選擇左側的“Downloads”
03 選擇對應的作業系統版本
04 傻瓜式安裝
05 [win10中若出現]安裝virtualbox快完成時立即回滾,並提示安裝出現嚴重錯誤
(1)開啟服務
(2)找到Device Install Service和Device Setup Manager,然後啟動
(3)再次嘗試安裝
#### 1.3 安裝centos7
01 建立centos7資料夾,並進入其中[目錄全路徑不要有中文字元]
02 在此目錄下開啟cmd,執行vagrant init centos/7
此時會在當前目錄下生成Vagrantfile,修改改檔案中的內容:
#指定映象
config.vm.box = "centos/7"
#設定網路模式,共享模式(與物理機共享網路)--即橋接
config.vm.network "public_network"
config.vm.provider "virtualbox" do |vb|
vb.memory = "3000" #設定虛擬機器佔用記憶體
vb.name= "centos7-zkx" #設定虛擬機器名稱
vb.cpus= 2 #虛擬機器佔用的cpu核數
end
同時指定使用的映象為centos/7,關鍵是這個映象在哪裡,我已經提前準備好了,名稱是virtualbox.box檔案
03 將virtualbox.box檔案新增到vagrant管理的映象中
(1)下載網盤中的virtualbox.box檔案
(2)儲存到磁碟的某個目錄,比如D:\virtualbox.box
(3)新增映象並起名叫centos/7:vagrant box add centos/7 D:\virtualbox.box
(4)vagrant box list 檢視本地的box[這時候可以看到centos/7]
04 centos/7映象有了,根據Vagrantfile檔案啟動建立虛擬機器
來到centos7資料夾,在此目錄開啟cmd視窗,執行vagrant up[開啟virtual box觀察,可以發現centos7建立成功]
05 以後大家操作虛擬機器,還是要在centos資料夾開啟cmd視窗操作
vagrant halt 優雅關閉
vagrant up 正常啟動
06 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.4 若想通過Xshell連線centos7
01 使用centos7的預設賬號連線
在centos資料夾下執行vagrant ssh-config
關注:Hostname Port IdentityFile
IP:127.0.0.1
port:2222
使用者名稱:vagrant
密碼:vagrant
檔案:Identityfile指向的檔案private-key
02 使用root賬戶登入
vagrant ssh 進入到虛擬機器中
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes
passwd修改密碼,比如abc123
systemctl restart sshd
使用賬號root,密碼abc123進行登入
#### 1.5 Vagrantfile通用寫法
#```ruby # -*- 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| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end config.vm.provider "virtualbox" do |vb| vb.memory = "4000" vb.name= "jack-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.6 box的打包分發
```
01 退出虛擬機器
vagrant halt
02 打包
vagrant package --output first-docker-centos7.box
03 得到first-docker-centos7.box
04 將first-docker-centos7.box新增到其他的vagrant環境中
vagrant box add first-docker-centos7 first-docker-centos7.box
05 得到Vagrantfile
vagrant init first-docker-centos7
06 根據Vagrantfile啟動虛擬機器
vagrant up [此時可以得到和之前一模一樣的環境,但是網路要重新配置]
```
### 2.1 安裝docker
> https://docs.docker.com/install/linux/docker-ce/centos/
```
01 進入centos7
vagrant ssh
02 解除安裝之前的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
03 安裝必要的依賴
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
04 設定docker倉庫 [設定阿里雲映象倉庫可以先自行百度,後面課程也會有自己的docker hub講解]
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
[訪問這個地址,使用自己的阿里雲賬號登入,檢視選單欄左下角,發現有一個映象加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors]
05 安裝docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
06 啟動docker
sudo systemctl start docker
07 測試docker安裝是否成功
sudo docker run hello-world
```
### 2.2 docker基本體驗
```
01 建立tomcat容器
docker pull tomcat
docker run -d --name my-tomcat -p 9090:8080 tomcat
02 建立mysql容器
docker run -d --name my-mysql -p 3301:3306 -e MYSQL_ROOT_PASSWORD=jack123 --privileged mysql
03 進入到容器裡面
docker exec -it containerid /bin/bash
```
### 2.3 可能有的疑惑
(1)docker pull在哪拉取的映象?
預設是在hub.docker.com
(2)docker pull tomcat拉取的版本是?
預設是最新的版本,可以在後面指定版本":"
(3)簡單先說一下命令咯
```
docker pull 拉取映象到本地
docker run 根據某個映象建立容器
-d 讓容器在後臺執行,其實就是一個程序
--name 給容器指定一個名字
-p 將容器的埠對映到宿主機的埠
docker exec -it 進入到某個容器中並互動式執行
```