当前位置: 首页 > news >正文

使用Vagrant 在Virtual Box 上安装Docker--(补充九步构建自己的hello world Docker镜像)

反思+记录

安装Virtual Box

Virtual Box 是和vmware, Hyper-v 一样的虚拟软件。
Virtual Box 下载地址 https://www.virtualbox.org/wiki/Downloads ;因在Windows,故选择Windows hosts 下载下来直接安装即可。

安装Vagrant

Vagrant 是一款可以结合 Virtual Box 进行虚拟机安装、 管理的软件,基于 Ruby ,因为已经编译为应用程序,所以可以不用安装 Ruby 环境。
Vagrant 下载地址:https://www.vagrantup.com/downloads.html;
在页面上选择对应平台及架构, 我选择Windows X64, 安装过程没有要注意的,可一直下一步。

通过 Vagrant 安装 centos7

以下操作都在 git bash 中进行,如果不是 git bash,自行手动进行或者通过CMD/PowerShell构建。

构建目录

在大空间磁盘上的某个目录中执行如下代码。

$ mkdir VM && cd VM && mkdir Vagrant
$ ll
total 4
-rw-r--r-- 1 Guzho 197609 3085 5月   9 08:41 Vagrantfile

安装CentOS7

$ vagrant init centos:7

......

通过此命令会在 Vagrant目录下生成一个 Vagrantfile 目录,这个目录记录了通过 Vagrant 安装的镜像的名称和版本。
大致内容如下, 对于我们有用的也就是没注释的那两行。

# -*- 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
  #
  # 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

安装过程

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' (v1803.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1803.                   01/providers/virtualbox.box
==> default: Box download is resuming from prior download progress
    default: Download redirected to host: cloud.centos.org
    default:
==> default: Successfully added box 'centos/7' (v1803.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1525826784695_31478
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host on                   ly
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work prope                   rly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/c/VM/Vagrant/ => /vagrant

查看Box

$ vagrant box list
centos/7 (virtualbox, 1803.01)

启动centos7

$ vagrant up

查看状态

$ vargant status

Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

在centos上安装Docker

参考地址:https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce

通过 vagrant 链接到刚才安装的centos,因为只有一个,所以命令行中可以不加此centos的名称。默认用户名和密码都是vagrant

$ vagrant ssh
Last login: Wed May  9 00:48:14 2018 from 10.0.2.2
[vagrant@localhost ~]$

清楚旧版本的Docker

$ 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

Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-selinux
No Match for argument: docker-engine-selinux
No Match for argument: docker-engine
No Packages marked for removal

添加Docker所需依赖

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Loaded plugins: fastestmirror
base                                                                        | 3.6 kB  00:00:00
extras                                                                      | 3.4 kB  00:00:00
updates                                                                     | 3.4 kB  00:00:00
(1/4): extras/7/x86_64/primary_db                                           | 185 kB  00:00:00
(2/4): base/7/x86_64/group_gz                                               | 156 kB  00:00:00
(3/4): base/7/x86_64/primary_db                                             | 5.7 MB  00:00:01
(4/4): updates/7/x86_64/primary_db                                          | 6.9 MB  00:00:02
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
Package yum-utils-1.1.31-42.el7.noarch already installed and latest version
Package device-mapper-persistent-data-0.7.0-0.1.rc6.el7_4.1.x86_64 already installed and latest version
Package 7:lvm2-2.02.171-8.el7.x86_64 already installed and latest version
Nothing to do

添加Docker源

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

安装最新的 Docker-ce

$ sudo yum install docker-ce

# 安装期间提示所有的都选择y,回车即可
......
Installed:
  docker-ce.x86_64 0:18.03.1.ce-1.el7.centos

Dependency Installed:
  audit-libs-python.x86_64 0:2.7.6-3.el7             checkpolicy.x86_64 0:2.5-4.el7
  container-selinux.noarch 2:2.42-1.gitad8f0f7.el7   libcgroup.x86_64 0:0.41-13.el7
  libsemanage-python.x86_64 0:2.5-8.el7              libtool-ltdl.x86_64 0:2.4.2-22.el7_3
  pigz.x86_64 0:2.3.3-1.el7.centos                   policycoreutils-python.x86_64 0:2.5-17.1.el7
  python-IPy.noarch 0:0.75-6.el7                     setools-libs.x86_64 0:3.3.8-1.1.el7

Complete!

启动Docker

sudo systemctl start docker

解决权限问题

$ sudo groupadd docker
groupadd: group 'docker' already exists
$ sudo gpasswd -a vagrant docker
Adding user vagrant to group docker
$ exit
logout
Connection to 127.0.0.1 closed.
$ vagrant ssh
Last login: Wed May  9 01:43:12 2018 from 10.0.2.2
$ 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

看到 Client 和 Server 都有显示,说明Docker启动正常。

运行 hello-world

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

看到有 Hello from Docker! 输出即可证明 Docker 安装正确并能运行。

总结

利用Virtual Box和 Vagrant 两个工具的便利性,我们可以简单的就完成虚拟机镜像的管理及Docker的安装、创建及其他控制。Docker 对 vmware 的支持需要特殊的扩展支持,比较繁琐,对于学习和实验阶段来说Virtual Box是最好的选择,当然还有Hyper-v。如果有Hyper-v,也可以使用docker-machine 来管理Docker。

相关文章:

  • Linux下PHP5.2安装curl扩展支持https
  • 分布式架构总汇【转】
  • MFS分布式文件系统部署
  • 阿里云Elasticsearch公测发布
  • Django中Model-Form验证
  • win10 常用设置 桌面出来计算机图标,固定桌面摆好的图标设置方法,电脑设备ID方法...
  • D的去世给我的震撼
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • CAP理论的例子讲解
  • 欧拉函数 + 线性求法
  • 编写高质量JavaScript代码之并发
  • Python成长之路【第三篇】函数
  • Callable和Future用法示例
  • 谁说我们IT不重要???
  • linux-NAT连接外网
  • 【Leetcode】104. 二叉树的最大深度
  • 345-反转字符串中的元音字母
  • codis proxy处理流程
  • CSS3 变换
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • CSS进阶篇--用CSS开启硬件加速来提高网站性能
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • Hibernate最全面试题
  • Js基础知识(一) - 变量
  • MySQL-事务管理(基础)
  • Python3爬取英雄联盟英雄皮肤大图
  • Redis的resp协议
  • 简单基于spring的redis配置(单机和集群模式)
  • 聊聊hikari连接池的leakDetectionThreshold
  • 如何利用MongoDB打造TOP榜小程序
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 数据仓库的几种建模方法
  • 赢得Docker挑战最佳实践
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • ​学习一下,什么是预包装食品?​
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #Java第九次作业--输入输出流和文件操作
  • #WEB前端(HTML属性)
  • $ git push -u origin master 推送到远程库出错
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (七)Knockout 创建自定义绑定
  • (转)德国人的记事本
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .net 设置默认首页
  • .net 使用ajax控件后如何调用前端脚本
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .NET和.COM和.CN域名区别
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • ??在JSP中,java和JavaScript如何交互?
  • @property python知乎_Python3基础之:property