maxhallinan
maxhallinan

Reputation: 1329

Vagrant curl connection refused while provisioning

I have CouchDB running on a Vagrant box. I want to create the database when provisioning the box. To create the db, I use curl to make a PUT to 127.0.0.1:5984/foo. If I do this manually, no problem. SSH-ing into the Vagrant box and running the command works. Running the command on the host machine works. But if I have the provisioning script run the command, I get this error: curl: (7) Failed to connect to 127.0.0.1 port 5984: Connection refused. What is the problem?

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 5984, host: 5984, host_ip: "127.0.0.1"
  config.vm.provision "shell", path: "./scripts/provision_vagrant.sh"
end

Upvotes: 0

Views: 863

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53713

There's 2 thing:

First, when you run your command after you ssh'ing into the box, you will run the command as vagrant user but when you run your provisioning as you shown you actually run the command as root; so to respect what you did when ssh'ing make sure to run with privileged: false argument as follow:

config.vm.provision "shell", privileged: false, path: "./scripts/provision_vagrant.sh"

consider this when you write your shell script as if you run as root, you dont need the sudo

Second, when starting a process such as couchdb it takes a bit of time (few seconds to a minute depending hardware) to completely start and be available, this is totally fine and its the same for most process (mysql, apache) so when running the curl from your script it might be running while couch is starting up and not fully available.

The easiest is to sleep just enough so that the server is up - the most sophisticated version is to read the output log file until it says it has started.

Upvotes: 1

Related Questions