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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Serverless Kubernetes 快速入门指南
5月,阿里云宣布推出Serverless Kubernetes服务。开发者可在5秒内创建集群、30秒部署应用上线。用户无需管理集群基础设施,根据应用实际消耗资源按量付费;用户可以直接使用K8S API或命令行来管理容器应用,容器应用可以与VPC中现有应用和阿里云能力无缝集成。此举可以进一步降低容器技术的使用门槛,让用户关注在应用构建而非基础架构运维。 我们提供一些示例,帮助大家快速上手。 通过控制台部署一个机器学习的示例应用 利用 kubectl 直接部署、管理容器应用 使用 CodePipeline 实现CI/CD, 详情 更多示例 可以参考 https://github.com/AliyunContainerService/serverless-k8s-examples 利用 Init Container 部署 tomcat 应用 利用 Si
- 下一篇
运维工程师需要掌握的技能
提起运维,大家都会想到又苦又累,又不被人重视的网管,机房运维,认为这个工种很low,入门门槛低,做的事比较杂,从搬服务器上架到修打印机,修电脑,处理网络小故障等杂七杂八的活,究其原因,多年来小公司都把运维当做一种打杂的小工使。其实运维从分类可以分为很多专业领域,比如网络工程师,系统工程师,备份存储工程师,CDN工程师,数据库管理员,应用运维,运维开发,运维架构师等等岗位,本章暂不对运维岗位做过多解释,我就个人的经验,我是从运维实施、系统管理员、DBA最后到运维开发的,简单说说各大互联网公司对岗位的面试要求: WEB篇:1、负载均衡篇:熟悉常用的负载均衡技术,LVS,dpdk,Nginx等四层/七层负载均衡,除了硬件负载均衡设备F5,有搭建、维护并熟悉负载均衡原理;2、Nginx 重要配置、优化及使用、与lua的结合,接入、降级、限流、隔离、熔断、动静分离、反向代理、负载均衡,反爬虫,黑白名单等 另Nginx 和Apache的比较;3、TCP/HTTPs协议及三次握手,四次挥手,get/post的差异,协议头,跳转,常见返回码的含义200/30x/40x/50x等知识及导致返回码的可能故...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7设置SWAP分区,小内存服务器的救世主
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装