标签:boot 独立 ref cpu man 平台 upload docker 镜像 虚拟
Docker是指容器化技术,用于支持创建和实验Linux Container。借助Docker,你可以将容器当做重量轻、模块化的虚拟机来使用,同时,你还将获得高度的灵活性,从而实现对容器的高效创建、部署和复制,并能将其从一个环境顺利迁移至另外一个环境。
Docker官方网站:http://www.docker.com
Github Docker 源码:https://github.com/docker/docker
基础特性:
Docker的应用场景
Docker基础架构图:
镜像image与容器Container区别:
Docker容器镜像站:https://hub.docker.com
Docker object:
首先安装之前先卸载之前的旧版本:
$ 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
安装docker依赖包:
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加docker源(如果Docker官方下载的速度太慢也可以使用阿里的镜像源:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo):
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker:
$ sudo yum install docker-ce
安装完直接启动docker即可:
~]# systemctl start docker
使用docker version
可以看到我们安装docker的版本
~]# docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false
Vagrant介绍
Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。我们可以使用它来干如下这些事:
Vagrant的运行,需要依赖某项具体的虚拟化技术,最常见的有VirtualBox以及VMWare两款,早期,Vagrant只支持VirtualBox,后来才加入了VMWare的支持。
Vagrant安装和使用
Vagrant是基于其他虚拟化产品来创建虚拟机的,这里我们选择开源产品Virtual Box,我们先安装Virtual Box和Vagrant:
下载好之后先使用vagrant init centos/7
生成默认的Vagrntfile,然后使用vagrant up
即可自动创建虚拟机,我们为了配置Docker环境需要更改下Vagrantfile,在创建虚拟机的时候自动安装docker环境(配置config.vm.provision "shell"
选项)并且按照自己的要求配置虚拟机的规模和网络。
我的Vagrantfile配置如下
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
boxes = [
{
:name => "docker-1",
:eth1 => "172.16.56.11",
:mem => "1024",
:cpu => "1"
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
end
config.vm.network :public_network, ip: opts[:eth1]
end
end
# config.vm.synced_folder "../sync", "/home/vagrant/sync"
config.vm.provision "shell", inline: <<-SHELL
yum -y install wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
sudo systemctl start docker
SHELL
end
之后使用vagrant up
创建即可
λ vagrant up
Bringing machine 'docker-1' up with 'virtualbox' provider...
==> docker-1: Importing base box 'centos/7'...
[KProgress: 20%
[KProgress: 40%
[KProgress: 90%
[K==> docker-1: Matching MAC address for NAT networking...
==> docker-1: Checking if box 'centos/7' is up to date...
==> docker-1: Setting the name of the VM: docker-1_docker-1_1534247086153_83300
==> docker-1: Clearing any previously set network interfaces...
==> docker-1: Preparing network interfaces based on configuration...
docker-1: Adapter 1: nat
docker-1: Adapter 2: bridged
==> docker-1: Forwarding ports...
docker-1: 22 (guest) => 2222 (host) (adapter 1)
==> docker-1: Running 'pre-boot' VM customizations...
==> docker-1: Booting VM...
==> docker-1: Waiting for machine to boot. This may take a few minutes...
docker-1: SSH address: 127.0.0.1:2222
docker-1: SSH username: vagrant
docker-1: SSH auth method: private key
docker-1:
docker-1: Vagrant insecure key detected. Vagrant will automatically replace
docker-1: this with a newly generated keypair for better security.
docker-1:
docker-1: Inserting generated public key within guest...
==> docker-1: Machine booted and ready!
==> docker-1: Checking for guest additions in VM...
...
在这个过程中Vagrant会帮我们创建好一台Centos 7的虚拟机并安装好Docker环境,使用vagrant ssh
直接登陆虚拟机
λ vagrant ssh
Last login: Wed Aug 15 15:54:32 2018 from 10.0.2.2
[vagrant@docker-1 ~]$ sudo docker version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:08:18 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:10:42 2018
OS/Arch: linux/amd64
Experimental: false
[vagrant@docker-1 ~]$
Vagrantfile中有一项config.vm.synced_folder
可以配置本地文件夹同步到虚拟机,挂载本地文件夹到虚拟机中需要VBoxAdditions的支持,这个可以在虚拟机起来之后在安装,也可以参考vagrant-vbguest这个插件的使用,这个功能比较适合开发人员,可以在本地编写代码而在Linux环境中调试代码运行。
Docker和vagrant都是最近才开始了解学习的,有什么错误或者不对的地方还请大家指正,有什么不懂的也可以一起交流。
标签:boot 独立 ref cpu man 平台 upload docker 镜像 虚拟
原文地址:https://www.cnblogs.com/zpzhue/p/9498024.html