# 安装
docker 的安装非常多教程,而且官方也有教程文档,可以移步:
https://docs.docker.com/get-docker/
但是 docker 底层本质还是调用的 linux kernel 内核来实现容器,总结来说就是
- 使用 namespace 来进行资源的隔离,包括:UTS,PID,IPC,NETWORK,MOUNT,USER
- 使用 cgroup 进行资源的限制,包括:内存,cpu等等
- 使用 rootfs 和 mount 来打造隔离容器的文件系统
如果你是单纯使用 docker,当然可以直接按照官网推荐方式来进行安装,直接运行在 docker machine 上,不用关心底层细节,但是如果你需要深究docker底层细节,还是建议在linux上进行安装。
#install some tools sudo yum install -y git vim gcc glibc-static telnet bridge-utils net-tools #install docker curl -fsSL get.docker.com -o get-docker.sh sh get-docker.sh #开机启动 sudo systemctl enable docker.service # start docker service sudo systemctl start docker #将当前用户添加到docker组下,避免sudo执行docker sudo groupadd docker sudo gpasswd -a vagrant docker sudo systemctl restart docker.service rm -rf get-docker.sh
如果是本地学习使用,建议 vgrant + virtualBox 的方式安装虚拟机,然后再在其上面进行安装 docker
# vgrant + virtualBox
版本要求:
- 下载最新的 virtualBox
- 下载最新的 vagrant (要求最低版本为2.2)
新增 vagrant centos7 Box 镜像:
# 初始化 centos 当前目录下生成 VagrantFile 配置文件 vagrant init centos/7 # 但是一般从 vagrantCloud 下载都会很慢,所以可以提前下载放到本地 # 前往 https://cloud.centos.org/centos/7/vagrant/x86_64/images/ # 下载对应的VirtualBox.box文件。 # 放到 vagrant 的boxes目录下 ~/.vagrant.d/boxes cd /Users/${username}/.vagrant.d/boxes # 然后使用命令 vagrant box add centos/7 ~/.vagrant.d/CentOS-7-x86_64-Vagrant-1804_02.VirtualBox.box
安装虚拟机:
# 解决挂载磁盘的问题 # 报错 No Virtualbox Guest Additions installation found. # 更改 gem 源地址 $ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/ $ gem sources -l https://gems.ruby-china.com # 安装插件 vagrant plugin install vagrant-vbguest --plugin-clean-sources --plugin-source https://gems.ruby-china.com/ # 安装 vagrant up
vagrant 挂载 mount 的时候后面会有这样的问题,不知道其他的版本有没有,先记录下来解决方法
umount: /mnt: not mounted ==> yy-vm: Checking for guest additions in VM... yy-vm: No guest additions were detected on the base box for this VM! Guest yy-vm: additions are required for forwarded ports, shared folders, host only yy-vm: networking, and more. If SSH fails on this machine, please install yy-vm: the guest additions and repackage the box to continue. yy-vm: yy-vm: This is not an error message; everything may continue to work properly, yy-vm: in which case you may ignore this message. The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! umount /mnt Stdout from the command: Stderr from the command: umount: /mnt: not mounted
解决方法是升级 vagrant 的插件
vagrant plugin uninstall vagrant-vbguest vagrant plugin install vagrant-vbguest --plugin-version 0.21
关于 docker 和 vagrant 的配置文件我一般是这样配置的:
文件目录
. ├── Vagrantfile ├── setup.sh └── workspace └── README.md
Vagrantfile
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.require_version ">= 1.6.0" boxes = [ { :name => "yy-vm", :eth1 => "192.168.5.140", :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 :private_network, ip: opts[:eth1] end end config.vm.synced_folder "./workspace", "/opt/htdocs2", create:true, type:"nfs" config.vm.provision "shell", privileged: true, path: "./setup.sh" end
setup.sh
#/bin/sh # install some tools sudo yum install -y git vim gcc glibc-static telnet # install docker curl -fsSL get.docker.com -o get-docker.sh # init docker origin source filePath="/etc/docker/daemon.json" if [ ! -f "$filePath" ];then mkdir -p /etc/docker/ touch $filePath fi cat>/etc/docker/daemon.json<<EOF { "registry-mirrors": ["https://registry.docker-cn.com"] } EOF sh get-docker.sh # start docker service sudo groupadd docker sudo gpasswd -a vagrant docker sudo systemctl enable docker.service sudo systemctl start docker.service rm -rf get-docker.sh