乐趣区

关于vagrant:从已有的vagrant虚拟机中打包镜像

应用 vagrant 的好一个益处就是能够想合作中分享咱们的 Box

打包

上面就介绍一下打包形式

首先确定须要打包哪台虚拟机,进入装置 VirtualBox 的装置目录

默认在这个门路 C:\Program Files\Oracle\VirtualBox

cd "C:\Program Files\Oracle\VirtualBox"

查看所有的虚拟机,能够看到有显示格局如下:"vmname" {vmid},咱们这里须要的是 vmname

> ./VBoxManage.exe list vms
"django-twitter_default_1619620682201_12310" {b3c74405-5d50-4f05-9f0e-98c9563f1fa5}

将指标切换到你须要的门路中,而后执行以下的目录

vagrant package --base django-twitter_default_1619620682201_12310 --output demo_ubuntu18.box

好了,这个时候你的门路下就会生成一个 demo_ubuntu18.box 的文件

应用打包的好的镜像

光有打包是不够的,因为打包的目标是为了应用 box 镜像。应用的时候有两个留神点,那就是因为 ssh 密钥的缘故,你 add,而后 up 之后会发现进不去, 这是就须要去批改 vagrantfile 这个文件,将默认的密钥登录改为账号密码登录。

ssh 密钥谬误如下

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/bionic64_demo_ubuntu18'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: twitter-term-1_default_1623336326135_46711
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
    default: 8000 (guest) => 80 (host) (adapter 1)
    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: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

然而账号密码登录不如密钥登录平安,所以第一次(账号 + 明码)登录之后,倡议改为密钥登陆

初始化

如果你还没有 vagrantfile 文件,才须要初始化操作

你能够复用老虚拟机的 vagrantfile 文件,或者执行初始化操作新建一个 vagrantfile 文件

vagrant init [box_name]

增加 box

vagrant box add {box_name} {box_file_path}

参考文章:

利用 vagrant 打包零碎 – 制作本人的 box
Vagrant+Miniconda+PyCharm

退出移动版