Search code examples
dockervagrantvagrant-provision

Vagrant: use provider and provision togather


This my main Vagrant file

Vagrant.configure(2) do |config|
  config.vm.define "app7" do |app7|
    app7.vm.synced_folder "/home/behrad/dunro","/var/www/html"
    app7.vm.network "forwarded_port", id: "ssh", guest: 22, host: 2222, auto_correct: true
    app7.vm.provider "docker" do |docker|
      docker.vagrant_vagrantfile = "dev/app7/Vagrantfile"
      docker.build_dir = "./dev/app7"
      docker.build_args = "-t","dunro/app7:20170701"
      docker.name = "app7"
      docker.ports = ['80:80']
      docker.has_ssh = true
    end
  end
end

and dev/app7/Vagrantfile

Vagrant.configure(2) do |config|
  config.vm.hostname = "app7"
  config.vm.provision "file", source: "keys/id_rsa.pub", destination: "/var/www/.ssh/authorized_keys"
  config.ssh.username = "www-data"                                 
  config.ssh.private_key_path = "keys/id_rsa"  
end

The dev/app7/Vagrantfile not working


Solution

  • The dev/app7/Vagrantfile not working

    Yes it is not working because its not a valid Vagrantfile.

    You have basically instructed vagrant to use host VM so from the main Vagrantfile, you' telling vagrant to look into another Vagrantfile to know the configuration of an host VM (the host VM that will run docker) so at minimum you need to have a config.vm.box settings that will be the base box for this VM

    An example of an host VM Vagrantfile will be

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    Vagrant.configure(2) do |config|
      config.vm.box = "ubuntu/trusty64"
    
      config.vm.hostname = "app7"
    
      config.vm.provision "shell", inline: "echo Hello, World"
    
      # make sure to have docker installed on this VM
      config.vm.provision "docker"
    
      config.vm.network :forwarded_port, guest: 80, host: 4567
    end
    

    This will create a VM based on ubuntu trusty64 (I make sure to install latest version of docker so the main Vagrantfile with docker provider will be able to run correctly)

    In this case I can see my inline shell provisioning running, the docker provision will install docker and then after the docker main provider will pull images on this VM