tmatilai / vagrant-proxyconf

Vagrant plugin that configures the virtual machine to use proxies
MIT License
531 stars 74 forks source link

Docker proxy conf not set the first time the provisioner is used (trusty64) #155

Closed mabako closed 5 years ago

mabako commented 8 years ago

Hey,

I've been trying to get the docker provisioner inside vagrant to work with vagrant-proxyconf, however the proxy conf seems to be set effectively only the second time the provisioner is used.

I've configured the proxy settings in my ~/.vagrant.d/Vagrantfile; and here's just the files I used to test:

Vagrantfile that won't work:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provider "virtualbox" do |v|
    v.memory = 300
    v.cpus = 2
  end

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

Vagrantfile that works:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.provider "virtualbox" do |v|
    v.memory = 300
    v.cpus = 2
  end

  config.vm.provision "docker" do |d|
  end

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

Version

Vagrant 1.8.5
vagrant-proxyconf (1.5.2)

Log

λ vagrant destroy -f && vagrant up                                                                                  
==> default: Forcing shutdown of VM...                                                                              
==> default: Destroying VM and associated drives...                                                                 
Bringing machine 'default' up with 'virtualbox' provider...                                                         
==> default: Importing base box 'ubuntu/trusty64'...                                                                
==> default: Matching MAC address for NAT networking...                                                             
==> default: Checking if box 'ubuntu/trusty64' is up to date...                                                     
==> default: Setting the name of the VM: pconftest_default_1475069885163_91737                                      
==> default: Clearing any previously set forwarded ports...                                                         
==> default: Clearing any previously set network interfaces...                                                      
==> default: Preparing network interfaces based on configuration...                                                 
    default: Adapter 1: nat                                                                                         
==> default: Forwarding ports...                                                                                    
    default: 22 (guest) => 2222 (host) (adapter 1)                                                                  
==> default: Running 'pre-boot' VM customizations...                                                                
==> 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: Remote connection disconnect. Retrying...                                                     
    default:                                                                                                        
    default: Vagrant insecure key detected. Vagrant will automatically replace                                      
    default: this with a newly generated keypair for better security.                                               
    default:                                                                                                        
    default: Inserting generated public key within guest...                                                         
    default: Removing insecure key from the guest if it's present...                                                
    default: Key inserted! Disconnecting and reconnecting using new SSH key...                                      
==> default: Machine booted and ready!                                                                              
==> default: Checking for guest additions in VM...                                                                  
    default: The guest additions on this VM do not match the installed version of                                   
    default: VirtualBox! In most cases this is fine, but in rare cases it can                                       
    default: prevent things such as shared folders from working properly. If you see                                
    default: shared folder errors, please make sure the guest additions within the                                  
    default: virtual machine match the version of VirtualBox you have installed on                                  
    default: your host and reload your VM.                                                                          
    default:                                                                                                        
    default: Guest Additions Version: 4.3.36                                                                        
    default: VirtualBox Version: 5.1                                                                                
==> default: Configuring proxy for Apt...                                                                           
==> default: Configuring proxy environment variables...                                                             
==> default: Mounting shared folders...                                                                             
    default: /vagrant => C:/Users/user/Documents/Projekte/pconftest                                               
==> default: Running provisioner: docker...                                                                         
    default: Installing Docker onto machine...                                                                      
==> default: Starting Docker containers...                                                                          
==> default: -- Container: hello-world                                                                              
The following SSH command responded with a non-zero exit status.                                                    
Vagrant assumes that this means the command failed!                                                                 

          rm -f /var/lib/vagrant/cids/fbb969117edfa916b86dfb67fd11decf1e336df0                                      
          docker run --cidfile=/var/lib/vagrant/cids/fbb969117edfa916b86dfb67fd11decf1e336df0 -d --name hello-world --restart=always hello-world                                                                                        

Stdout from the command:                                                                                            

Stderr from the command:                                                                                            

stdin: is not a tty                                                                                                 
Unable to find image 'hello-world:latest' locally                                                                   
Pulling repository docker.io/library/hello-world                                                                    
docker: Network timed out while trying to connect to https://index.docker.io/v1/repositories/library/hello-world/images. You may want to check your internet connection or if you are behind a proxy..                                  
See 'docker run --help'.                                                                                            
b6pzeusbc54tvhw5jgpyw8pwz2x6gs commented 7 years ago
config.vm.provision "shell", privileged: false,
  inline: "/bin/bash /home/ubuntu/baseImageProvisioner.sh"

The same problem. baseImageProvisioner.sh has a Docker installation script and Docker pull script, but that pull scripts didn't work despite the successful installation of Docker.

To avoid this problem, I have pick docker installation script out, and execute it first. like this:

config.vm.provision "shell", privileged: false,
  inline: "curl -fsSL https://get.docker.com/ | sh"

config.vm.provision "shell", privileged: false,
  inline: "/bin/bash /home/ubuntu/baseImageProvisioner.sh"
kutschkem commented 6 years ago

Can confirm, additionally I have found that calling vagrant provision after the initial vagrant up has failed also sets the proxy. I can also confirm that the workaround of having two provision blocks works.

My Vagrantfile:

  Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu/xenial64"
      config.disksize.size = "20GB"

      config.vm.network "private_network", ip: "192.168.50.4"

      config.vm.provider "virtualbox" do |v|
          v.name = "Ubuntu_docker_poc"
          v.memory = 2048
          v.customize ["modifyvm", :id, "--uartmode1", "disconnected"] # see https://bugs.launchpad.net/cloud-images/+bug/1627844
      end

      if Vagrant.has_plugin?("vagrant-proxyconf")
        config.proxy.http = "http://10.0.2.2:3128"  
        config.proxy.https = "http://10.0.2.2:3128"  
        config.proxy.no_proxy = "localhost,127.0.0.1"  
      end

      #set up keyboard layout (de)
      config.vm.provision "shell", inline: "sed -ie '/^XKBLAYOUT=/s/\".*\"/\"de\"/' /etc/default/keyboard && udevadm trigger --subsystem-match=input --action=change"

      config.vm.provision "docker" do |d|
        d.build_image "/vagrant/docker"
      end

    end
codylane commented 5 years ago

Ugh, yeah, I've started to see other's report this problem as well. There is no easy fix to this problem that I can see due to the way Vagrant uses the Middleware stack to provision a guest. I've seen lots of weird oddities around using the docker provisioner in the Vagrantfile and it all seems to be related to the middleware stack.

Here's a couple of issues that may be similar due to the Middleware in vagrant.

As a workaround, the proposed solution is the only solution that I've come up with which instead of using config.vm.provision "docker" to instead install docker via shell script.

I'd also recommend upgrading to 2.0.x as there are many new bug fixes.

codylane commented 5 years ago

This is a rather old issue and we have not received any further communication about it in a while. I'd recommend upgrading this plugin to the latest release. I hope to have 2.0.1 out very soon which I hope addresses this issue.

Please also see #189 where I've attempted to address docker related issues with this plugin.

Please open a new issue or re-open this issue if it's still broken after the 2.0.1 release is out.