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

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

反思+记录

安装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。

目录
相关文章
|
17天前
|
应用服务中间件 Linux nginx
Docker镜像-手动制作yum版nginx镜像
这篇文章介绍了如何手动制作一个基于CentOS 7.6的Docker镜像,其中包括下载指定版本的CentOS镜像,创建容器,配置阿里云软件源,安装并配置nginx,自定义nginx日志格式和web页面,最后提交镜像并基于该镜像启动新容器的详细步骤。
77 21
Docker镜像-手动制作yum版nginx镜像
|
17天前
|
应用服务中间件 nginx Docker
Docker镜像-基于DockerFile制作编译版nginx镜像
这篇文章介绍了如何基于Dockerfile制作一个编译版的nginx镜像,并提供了详细的步骤和命令。
93 17
Docker镜像-基于DockerFile制作编译版nginx镜像
|
17天前
|
应用服务中间件 Linux nginx
Docker镜像管理篇
关于Docker镜像管理的教程,涵盖了Docker镜像的基本概念、管理命令以及如何制作Docker镜像等内容。
77 7
Docker镜像管理篇
|
16天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
17天前
|
Ubuntu Linux Docker
Ubuntu 18.04 安装Docker实战案例
关于如何在Ubuntu 18.04系统上安装Docker的实战案例,包括安装步骤、配置镜像加速以及下载和运行Docker镜像的过程。
101 3
Ubuntu 18.04 安装Docker实战案例
|
17天前
|
存储 Linux Docker
CentOS 7.6安装Docker实战案例及存储引擎和服务进程简介
关于如何在CentOS 7.6上安装Docker、介绍Docker存储引擎以及服务进程关系的实战案例。
59 3
CentOS 7.6安装Docker实战案例及存储引擎和服务进程简介
|
17天前
|
应用服务中间件 Linux nginx
Docker镜像-基于DockerFile制作yum版nginx镜像
本文介绍了如何使用Dockerfile制作一个基于CentOS 7.6.1810的yum版nginx镜像,并提供了详细的步骤和命令。
58 20
|
4天前
|
Devops jenkins 持续交付
DevOps实践:构建和部署一个Docker化的应用
【9月更文挑战第14天】在当今快节奏的软件开发领域,DevOps已经成为提升效率、加速交付的关键。本文将引导你理解DevOps的核心概念,并通过一个实际的示例—构建和部署一个Docker化的应用—来深入探讨其实践方法。我们将从简单的应用出发,逐步实现Docker容器化,并最终通过CI/CD流水线自动化部署过程。这不仅是对DevOps流程的一次实操演练,也是对现代软件开发理念的一次深刻体验。
|
3天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。
|
6天前
|
Kubernetes Docker 微服务
构建高效的微服务架构:基于Docker和Kubernetes的最佳实践
在现代软件开发中,微服务架构因其灵活性和可扩展性而受到广泛青睐。本文探讨了如何利用Docker和Kubernetes来构建高效的微服务架构。我们将深入分析Docker容器的优势、Kubernetes的编排能力,以及它们如何结合实现高可用性、自动扩展和持续部署。通过具体的最佳实践和实际案例,读者将能够理解如何优化微服务的管理和部署过程,从而提高开发效率和系统稳定性。