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

目录
相关文章
|
22天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
122 42
|
15天前
|
监控 Linux PHP
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
65 20
|
15天前
|
Ubuntu API 网络虚拟化
ubuntu22 编译安装docker,和docker容器方式安装 deepseek
本脚本适用于Ubuntu 22.04,主要功能包括编译安装Docker和安装DeepSeek模型。首先通过Apt源配置安装Docker,确保网络稳定(建议使用VPN)。接着下载并配置Docker二进制文件,创建Docker用户组并设置守护进程。随后拉取Debian 12镜像,安装系统必备工具,配置Ollama模型管理器,并最终部署和运行DeepSeek模型,提供API接口进行交互测试。
241 15
|
14天前
|
消息中间件 Kafka Docker
docker compose 安装 kafka
通过本文的步骤,您可以快速在本地使用 Docker Compose 安装并配置 Kafka 和 Zookeeper。Docker Compose 简化了多容器应用的管理,方便快速搭建和测试分布式系统。
56 2
|
9月前
|
Ubuntu 网络安全 数据安全/隐私保护
百度搜索:蓝易云【docker通过dockerfile安装sftp教程。】
现在,你已经通过 Dockerfile 成功安装了 SFTP,并且可以使用指定的用户名和公钥进行远程访问。请确保替换示例中的用户名、密码和公钥为自己的实际值。
199 1
|
消息中间件 Shell RocketMQ
百度搜索:蓝易云 ,常用环境部署—Docker安装RocketMQ教程!
通过按照上述步骤,您可以在Docker中成功安装和部署RocketMQ。请注意,上述命令仅提供了一个基本的安装和配置过程,具体配置和使用可以根据您的需求进行进一步调整。确保在执行命令之前,您已经安装并配置好Docker环境。
204 0
|
3月前
|
缓存 Linux Docker
【最新版正确姿势】Docker安装教程(简单几步即可完成)
之前的老版本Docker安装教程已经发生了变化,本文分享了Docker最新版安装教程,其他操作系统版本也可以参考官 方的其他安装版本文档。
4179 3
【最新版正确姿势】Docker安装教程(简单几步即可完成)
|
4月前
|
数据可视化 数据挖掘 Docker
Docker Desktop 安装 ClickHouse 超级简单教程
Docker Desktop 安装 ClickHouse 超级简单教程
133 1
|
6月前
|
Linux 测试技术 Docker
Docker CE for CentOS 安装教程
Docker CE for CentOS 安装教程
186 1
|
7月前
|
关系型数据库 MySQL Linux
Docker安装mysql详细教程, mysqld: Can‘t read dir of ‘/etc/mysql/conf.d/‘(报错已解决)
Docker安装mysql详细教程, mysqld: Can't read dir of '/etc/mysql/conf.d/' (Errcode: 2 - No such file or directory) 已解决