文章目录
virtualbox+vagrant环境安装docker
一、软件版本
- 宿主机操作系统:windows 7 旗舰版
vagrant官网:https://www.vagrantup.com/
virtualbox官网:https://www.virtualbox.org/
docker官网:https://www.docker.com/
finalshell官网:http://www.hostbuf.com/
二、使用记录
1.下载并安装 virtual box
- (win7 略过)如果在
win10
中,安装时提示出现严重错误
服务 --> 启动
Device Install Service
和Device Setup Manager
服务 --> 重新安装
2.下载并安装 vagrant
3.创建 CentOS7 虚拟机
- 1)创建
docker-centos7
的文件夹,使用CMD
进入docker-centos7
目录 - 2)创建
Vagrantfile
,运行命令后,在docker-centos7
下生成
> vagrant init centos/7
- 3)修改
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|
# # 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 = "2048"
vb.cpus=2
vb.name="docker-centos7"
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
- 4)启动虚拟机,启动过程会下载
virtualbox.box
,为避免长时间等待,可以使用迅雷进行下载
> vagrant up
PS E:\ZacxWorkSpace\Vagrant\first-docker-centos7> vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Box ‘centos/7’ could not be found. Attempting to find and install…
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box ‘centos/7’
default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box ‘centos/7’ (v1905.1) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
default: Download redirected to host: cloud.centos.orgining: --:–:--)
-
使用迅雷下载:
https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
-
5)添加
virtualbox.box
到vagrant
中
> vagrant box add centos/7 D:/virtualbox.box
#-- 查看 vagrant box 的列表
> vagrant box list
- 6)启动虚拟机,这时候会在
virtual box
中会自动添加一个虚拟机
> vagrant up
4.使用 FinalShell 连接虚拟机
- 查看虚拟机配置(关注:HostName、User、Port、IdentityFile)
> vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile E:/Vagrant/first-docker-centos7/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
- 打开软件:
FinalSheel
认证:
方法:选择
公钥
用户名:vagrant
密码:vagrant
---- 如果失败:使用以下方式
认证:
方法:选择
公钥
用户名:vagrant
私钥:选择上面
IdentityFile
的文件
- 修改
ssh
的配置
# sudo -i
# vi /etc/ssh/sshd_config
#-- 将 PasswordAuthentication 修改为 yes
PasswordAuthentication yes
- 设置密码
# passwd
- 重启
sshd
服务
systemctl restart sshd
- 在
FinalShell
中,端口为:22,使用root
用户和上面设置的密码进行登录
5.安装 docker
官网安装说明: https://docs.docker.com/install/linux/docker-ee/centos/
- 1)
FinalShell
连接到虚拟机 - 2)卸载之前的版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
- 3)安装必要的依赖
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
- 4)设置
docker
仓库(阿里云的仓库)
$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 5)安装
docker
$ sudo yum -y install docker-ee docker-ee-cli containerd.io
- 6)启动 docker,并设置开机自启动
$ sudo systemctl start docker && sudo systemctl enable docker
- 7)测试 docker 是否启动成功
$ sudo docker run hello-world
6.docker使用tomcat和mysql
- tomcat
#-- 从远处镜像仓库下载tomcat的image,默认最新版本,可以知道版本,如:tomcat:8
$ docker pull tomcat
#-- 运行镜像,创建容器, -d=静默模式;--name=容器名称
#-- ;-p=将容器的8080端口,映射到centos的9090端口上
$ docker run -d --name my-tomcat -p 9090:8080 tomcat
- mysql
#-- 从远处镜像仓库下载tomcat的image,默认最新版本,可以知道版本,如:mysql:5.7
$ docker pull mysql
#-- 运行镜像,创建容器, -d=静默模式;--name=容器名称
#-- -p=将容器的3306端口,映射到centos的3316端口上
#-- MYSQL_ROOT_PASSWORD=设置mysql密码;--privileged=获取centos的root权限
$ docker run -d --name my-mysql -p 3316:3306 -e MYSQL_ROOT_PASSWORD=root123 --privileged mysql
- 进入到容器,进行交互式操作
$ docker exec -it [CONTAINER ID] /bin/bash
三、其它
PowerShell版本过低
- 低版本的
PowerShell
和cmd
运行命令时,会出现报错信息
> vagrant
Vagrant failed to initialize at a very early stage:
The version of powershell currently installed on this host is less than
the required minimum version. Please upgrade the installed version of
powershell to the minimum required version and run the command again.Installed version: N/A
Minimum required version: 3
解决方法:
- 安装最新版
PowerShell
:https://docs.microsoft.com/en-us/powershell/
- 安装其中的
.msu
,重启系统即可
- 安装其中的
PowerShell
中查询版本信息:
> Get-Host | Select-Object Version
vagrant 常用命令
- 官网命令说明: https://www.vagrantup.com/docs/cli/
命令 | 说明 |
---|---|
vagrant box list | 查看 vagrant 的 box 列表 |
vagrant box add [box名称] [.box文件的路径] | 新增一个box到vagrant |
vagrant box remove [box名称] | 删除指定的box |
vagrant init [非必填,box名称] | 生成 Vagrantfile |
vagrant up | 启动虚拟机 |
vagrant ssh | ssh登录虚拟机 |
vagrant suspend | 挂起虚拟机 |
vagrant reload | 重启虚拟机 |
vagrant halt | 关闭虚拟机 |
vagrant status | 查看虚拟机状态 |
vagrant destroy | 删除虚拟机 |
docker 常用命令
- 官网命令说明: https://docs.docker.com/engine/reference/commandline/cli/
命令 | 说明 |
---|---|
docker ps -a | 查询容器信息 |
docker start [CONTAINER ID] | 根据[容器id],启动容器 |
vagrant box的系统打包
1)退出虚拟机
> vagrant halt
2)打包
> vagrant package --output out-docker-centos7.box
3)将导出的 out-docker-centos7.box
,迁移到其它机器的 vagrant
环境中
> vagrant box add docker-centos7 out-docker-centos7.box
4)生成 Vagrantfile
文件
> vagrant init docker-centos7
5)根据Vagrantfile
文件,启动虚拟机
> vagrant up