【问题标题】:Best way to install docker on Vagrant在 Vagrant 上安装 docker 的最佳方法
【发布时间】:2017-11-21 14:37:20
【问题描述】:

我想创建几个预装了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 镜像?

【问题讨论】:

    标签: docker vagrant vagrant-provision


    【解决方案1】:

    这里是更人性化的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
    

    【讨论】:

    • 好东西,非常感谢。我唯一要补充的是,在已经配置好的盒子上,重新加载(我认为与停止 + 向上相同)没有运行新的配置命令;在我的情况下,配置需要很长时间,而且我不确定--provision 是否需要同样长的时间,我添加了:run => 'always' 例如config.vm.provision :docker, :run => 'always' 以强制运行 docker 配置。
    • 它对我有用,谢谢:安装插件。 $ vagrant plugin install vagrant-docker-compose检查插件安装$ vagrant plugin list
    【解决方案2】:

    如果您正在使用支持 Docker provisioner 的最新 Vagrant(例如,以下步骤是使用 2.2.6 测试的),那么您可以使用非常简单的一两个单行代码安装 Docker,而无需 d.run or similar hacks

    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
    

    【讨论】:

      【解决方案3】:

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

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

      来自this StackOverflow answer:如果你想安装特定版本的 docker,你必须在你的 docker provisioner 之前运行一个 shell provisioner(provisioners 是按顺序运行的)来安装一个特定版本的 Docker。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 它使用 boot2docker 也不是 vagrant
        【解决方案5】:

        官方说明在这里: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
        

        【讨论】:

          【解决方案6】:

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

          也许有一天,你需要将 docker 部署到另一个地方,这个脚本会对你有很大帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-08-03
            • 2015-06-08
            • 2015-07-19
            • 2019-08-08
            • 2013-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多