vagrant+virtualbox搭建centos7然后安装Docker

一、vagrant安装

1、访问Vagrant官网

https://www.vagrantup.com/
vagrant+virtualbox搭建centos7然后安装Docker_第1张图片

2、点击Download

选择对应的版本
vagrant+virtualbox搭建centos7然后安装Docker_第2张图片

3、傻瓜式安装

一直点下一步,下一步即可。完成安装。

4、测试是否安装成功

输入命令 vagrant

vagrant

vagrant+virtualbox搭建centos7然后安装Docker_第3张图片

二、virtualbox安装

1、访问官网

https://www.virtualbox.org/

vagrant+virtualbox搭建centos7然后安装Docker_第4张图片

2、选择对应的版本

vagrant+virtualbox搭建centos7然后安装Docker_第5张图片

3、傻瓜式安装

一直点下一步即可。我在安装的过程中,出现了一个严重错误,导致安装失败。如果你也是的话,可以参考下面这篇文章。
https://blog.csdn.net/weixin_45119323/article/details/111321589

三、安装centos7

1、创建centos7文件夹

vagrant+virtualbox搭建centos7然后安装Docker_第6张图片

2、命令行进入centos7

打开cmd进入centos7文件夹里。执行 vagrant init centos/7 命令

vagrant init centos/7

打开文件夹,会发现文件夹里新生成了一个Vagrantfile文件

3、创建virtualbox.box文件

新建一个virtualbox.box文件,可以直接从以下网盘下载。

链接:https://pan.baidu.com/s/1Cy7YUlOtzRYlKcQtoaJE5Q
提取码:ddt7

4、将virtualbox.box文件添加到vagrant管理的镜像中

执行命令 vagrant box add centos/7 D:\virtualbox.box

//后面是virtualbox.box在磁盘中的路径
vagrant box add centos/7 D:\virtualbox.box

查看是否添加成功

vagrant box list

5、开启centos虚拟机

在之前创建的centos7文件夹打开cmd命令行。开启centos虚拟机

vagrant up    启动
vagrant halt    关闭

vagrant+virtualbox搭建centos7然后安装Docker_第7张图片
如果你执行vagrant up 命令报错,可看下面这篇文章
https://blog.csdn.net/weixin_45119323/article/details/111355451

6、修改Vagrantfile

# -*- 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
    config.vm.provider "virtualbox" do |vb|
        vb.memory = "4000"
        vb.name= "jack-centos7"
        vb.cpus= 2
    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

修改后,执行vagrant halt关闭虚拟机,然后再执行vagrant up重启一下。

vagrant reload  用来刷新

7、vagrant 相关命令

(1)vagrant ssh
进入刚才创建的centos7中
(2)vagrant status
查看centos7的状态
(3)vagrant halt
停止/关闭centos7
(4)vagrant destroy
删除centos7
(5)vagrant status
查看当前vagrant创建的虚拟机

vagrant+virtualbox搭建centos7然后安装Docker_第8张图片

四、使用Xshell连接虚拟机

1、下载安装XShell

下载安装Xshell,或者其他的远程连接软件,这里不作概述

2、使用默认账号登录

在centos文件夹下执行vagrant ssh-config
vagrant+virtualbox搭建centos7然后安装Docker_第9张图片

3、使用root账户登录

1、vagrant ssh 进入到虚拟机中

2、sudo -i 转为root账户

3、vi /etc/ssh/sshd_config 进入文件

4、 修改PasswordAuthentication yes

5、 passwd修改密码,比如abc123
6、 systemctl restart sshd 刷新

7、 使用账号root,密码abc123进行登录
vagrant+virtualbox搭建centos7然后安装Docker_第10张图片
主机号就是这个,看看自己的地址是多少
vagrant+virtualbox搭建centos7然后安装Docker_第11张图片

五、安装Docker

1、 进入centos7

	vagrant ssh

2、卸载之前的docker

	sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

vagrant+virtualbox搭建centos7然后安装Docker_第12张图片

3、安装必要的依赖

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

vagrant+virtualbox搭建centos7然后安装Docker_第13张图片

4、 设置docker仓库
sudo yum-config-manager
–add-repo
https://download.docker.com/linux/centos/docker-ce.repo
vagrant+virtualbox搭建centos7然后安装Docker_第14张图片

访问这个地址,使用自己的阿里云账号登录,查看菜单栏左下角,发现有一个镜像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

vagrant+virtualbox搭建centos7然后安装Docker_第15张图片
vagrant+virtualbox搭建centos7然后安装Docker_第16张图片

05 安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
vagrant+virtualbox搭建centos7然后安装Docker_第17张图片

06 启动docker
sudo systemctl start docker

设置开机启动
sudo systemctl enable docker

vagrant+virtualbox搭建centos7然后安装Docker_第18张图片

07 测试docker安装是否成功
sudo docker run hello-world
vagrant+virtualbox搭建centos7然后安装Docker_第19张图片
大功告成。

你可能感兴趣的:(环境搭建,docker,centos,ssh)