在Vagrant上安装docker的最佳方法

时间:2017-11-21 14:37:20

标签: docker vagrant vagrant-provision

我想创建几个预安装docker的虚拟机。

最佳/推荐的方法是什么?

a)让docker配置程序执行虚拟操作,以便安装docker,例如

  mymachine.vm.provision "docker" do |docknode|
      # do something pointless
  end

b)通过shell配置程序脚本运行docker安装吗?

mymachine.vm.provision "shell", path: "docker-installation-script.sh"

c)使用预先安装docker附带的Vagrant图像?

6 个答案:

答案 0 :(得分:4)

如果您使用的是具有Docker provisioner支持的最新Vagrant(例如,以下步骤已使用2.2.6进行了测试),则可以使用非常简单的一两个衬垫来安装Docker,而无需使用{{ 3}}:

Vagrant.configure(2) do |config|
  config.vm.box = "generic/ubuntu1904"

  # Install Docker
  config.vm.provision :docker

  # Install Docker Compose
  # First, install required plugin https://github.com/leighmcculloch/vagrant-docker-compose:
  # vagrant plugin install vagrant-docker-compose
  config.vm.provision :docker_compose

end

运行vagrant provisionvagrant up并观察以下输出:

==> default: Running provisioner: docker...
    default: Installing Docker onto machine...
==> default: Running provisioner: docker_compose...
    default: Checking for Docker Compose installation...
    default: Getting machine and kernel name from guest machine...
    default: Downloading Docker Compose 1.24.1 for Linux x86_64
    default: Uploading Docker Compose 1.24.1 to guest machine...
    default: Installing Docker Compose 1.24.1 in guest machine...
    default: Symlinking Docker Compose 1.24.1 in guest machine...

最后,vagrant ssh到VM并检查已部署的Docker基础架构的版本:

$ docker --version
Client: Docker Engine - Community
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        a872fc2f86
 Built:             Tue Oct  8 01:00:44 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.3
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       a872fc2f86
  Built:            Tue Oct  8 00:59:17 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

答案 1 :(得分:3)

这里Vagrantfile更加用户友好(在Vagrant 2.2.7上进行了测试)

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/bionic64"

  # require plugin https://github.com/leighmcculloch/vagrant-docker-compose
  config.vagrant.plugins = "vagrant-docker-compose"

  # install docker and docker-compose
  config.vm.provision :docker
  config.vm.provision :docker_compose

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ioapic", "on"]
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.customize ["modifyvm", :id, "--cpus", "2"]
  end

end

here所述,您可以要求在Vagrantfile内安装插件

这是步骤

$ vagrant up
Vagrant has detected project local plugins configured for this
project which are not installed.

  vagrant-docker-compose
Install local plugins (Y/N) [N]: y
Installing the 'vagrant-docker-compose' plugin. This can take a few minutes...
Fetching: vagrant-docker-compose-1.5.1.gem (100%)
Installed the plugin 'vagrant-docker-compose (1.5.1)'!

Vagrant has completed installing local plugins for the current Vagrant
project directory. Please run the requested command again.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/bionic64'...
...
==> default: Running provisioner: docker...
    default: Installing Docker onto machine...
==> default: Running provisioner: docker_compose...
    default: Checking for Docker Compose installation...
    default: Getting machine and kernel name from guest machine...
    default: Downloading Docker Compose 1.24.1 for Linux x86_64
    default: Downloaded Docker Compose 1.24.1 has SHA256 signature cfb3...
    default: Uploading Docker Compose 1.24.1 to guest machine...
    default: Installing Docker Compose 1.24.1 in guest machine...
    default: Symlinking Docker Compose 1.24.1 in guest machine...

$ vagrant ssh
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-88-generic x86_64)
vagrant@ubuntu-bionic:~$ docker -v
Docker version 19.03.8, build afacb8b7f0
vagrant@ubuntu-bionic:~$ docker-compose -v
docker-compose version 1.24.1, build 4667896b

答案 2 :(得分:0)

我使用docker-machine作为“您可以使用Machine在本地Mac或Windows机器上,公司网络上,数据中心或Azure,AWS等云提供商上创建Docker主机数字海洋“。这是一种使用Docker在内部启动VM的简单快捷的方法。

答案 3 :(得分:0)

官方指示在这里:https://www.vagrantup.com/docs/provisioning/docker.html

例如:

Vagrant.configure("2") do |config|
  config.vm.provision "docker" do |d|
    d.build_image "/vagrant/app"
  end
end

或者

Vagrant.configure("2") do |config|
  config.vm.provision "docker" do |d|
    d.run "rabbitmq"
  end
end

答案 4 :(得分:0)

我也在寻找这个问题的答案,并且在StackOverflow上找到了this answer。看来您是对的。运行虚拟映像是安装最新版本的DOCKER的最佳方法。

  config.vm.provision "docker" do |d|
      d.run "hello-world"
  end

来自this StackOverflow answer:如果要安装特定版本的Docker,则必须在运行Docker自动配置器(按顺序运行预配器)之前运行Shell自动配置器,以安装特定版本的Docker。

答案 5 :(得分:0)

我认为(b)是最好的部署方式,这样您就可以知道在此期间发生了什么。这意味着,当发现错误或想要的某些功能时,您可以解决所有问题。

有一天,也许您需要将docker部署到另一个地方,该脚本将为您提供很多帮助。