使用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。

目录
相关文章
|
4天前
|
存储 JSON 数据格式
docker load 后镜像名称为空的问题解决
Docker在容器化应用程序时提供了强大的镜像管理功能,但也可能在某些操作中遇到如镜像名称为空的问题。通过理解问题的成因并采取适当的解决方案,如正确保存和加载镜像、手动修复标签等,可以有效避免和解决这一问题。通过本文提供的指导,您可以确保在使用Docker进行镜像操作时更为顺利,并提高容器管理的效率。
97 82
|
6天前
|
监控 关系型数据库 MySQL
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
zabbix7.0.9安装-以宝塔安装形式-非docker容器安装方法-系统采用AlmaLinux9系统-最佳匹配操作系统提供稳定运行环境-安装教程完整版本-优雅草卓伊凡
79 30
|
6天前
|
SQL Linux 数据库
YashanDB Docker镜像制作
本文介绍了使用Docker部署YashanDB数据库的方法及其优势。相比传统部署方式,Docker简化了环境配置,实现一键部署,确保软件在不同环境中一致运行。文章详细讲解了数据库镜像与容器的概念、Dockerfile的构建流程,以及如何通过Dockerfile定制YashanDB镜像。此外,还演示了镜像的发布过程,包括推送至阿里云容器镜像服务(ACR)。最后,探讨了容器启动时的初始化脚本设置和数据文件复用方法,满足客户对数据库自动化初始化和数据持久化的需求。
|
17天前
|
存储 虚拟化 Docker
Docker Desktop 4.38 安装与配置全流程指南(Windows平台)
Docker Desktop 是容器化应用开发与部署的一体化工具,支持本地创建、管理和运行 Docker 容器。4.38 版本新增 GPU 加速、WSL 2 性能优化和 Kubernetes 1.28 集群管理功能,适用于微服务开发和 CI/CD 流水线搭建。安装要求为 Windows 10 2004 及以上(64 位),需启用 Hyper-V 或 WSL 2。硬件最低配置为 4GB 内存、20GB 存储和虚拟化技术支持的 CPU。安装步骤包括启用系统功能、下载并运行安装程序,完成后配置镜像加速并验证功能。常见问题涵盖 WSL 2 安装不完整、磁盘空间清理及容器外网访问等。
1022 12
|
9天前
|
Ubuntu Linux Docker
Docker 入门全攻略:安装、操作与常用命令指南
Docker 的世界非常广阔,这只是一个开始,请继续探索和学习 Docker 的高级特性和最佳实践。后续也会继续更新相关的理论与实践内容。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
11天前
|
Ubuntu Linux Docker
如何在Ubuntu 20.04系统中安装Docker
安装 Docker 引擎的步骤如下:首先更新系统包索引 (`sudo apt update`),安装必要依赖包 (`apt-transport-https` 等),添加 Docker 官方 GPG 密钥及 APT 仓库。接着再次更新包索引并安装 Docker 引擎及相关工具 (`docker-ce` 等)。最后启动 Docker 服务并设置开机自启,通过 `docker --version` 和运行测试容器 (`sudo docker run hello-world`) 验证安装是否成功。
97 0
|
Linux Docker Windows
Docker概述:什么Docker如此特别,它是如何构建的
本文讲的是Docker概述:什么Docker如此特别,它是如何构建的,【编者的话】在上一系列介绍中,我们大体地介绍了CoreOS。在这个系列中我想介绍一下Docker。本文我们看一下为什么Docker如此特别,它是如何构建的。我们会从基本开始介绍,所以读者不必担心自己是刚接触Docker的。
1392 0
|
5天前
|
存储 虚拟化 Docker
|
2月前
|
数据库 Docker 容器
docker容器为啥会开机自启动
通过配置适当的重启策略,Docker容器可以在主机系统重启后自动启动。这对于保持关键服务的高可用性和自动恢复能力非常有用。选择适合的重启策略(如 `always`或 `unless-stopped`),可以确保应用程序在各种情况下保持运行。理解并配置这些策略是确保Docker容器化应用可靠性的关键。
317 93
|
5天前
|
开发工具 虚拟化 git
自学软硬件第755 docker容器虚拟化技术youtube视频下载工具
docker容器虚拟化技术有什么用?怎么使用?TubeTube 项目使用youtube视频下载工具