guy mograbi
guy mograbi

Reputation: 28608

How to run provision.sh on vagrant with docker provider

I have used vagrant successfully on many clouds now.

I have decided to try something with docker, but I am probably missing something very basic as I am failing tremendously.

I wrote this vagrant file:

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

  config.vm.synced_folder "../synced_folder", "/vagrant"

  config.vm.provision "shell" do |s|
    s.path = "provision.sh"
    s.privileged = false
  end

  config.vm.provider :docker do |docker, override|
    docker.image = 'ubuntu'

  end
end

this configuration worked for me for other clouds (if I add specific cloud details to it of course).

my provision.sh file simply has echo "hello world"

my synced_folder has a dummy file..

I have verified that ubuntu image is working fine in docker. docker seems to be working fine.

When I run vagrant up --provider docker I get the following

The container started either never left the "stopped" state or
very quickly reverted to the "stopped" state. This is usually
because the container didn't execute a command that kept it running,
and usually indicates a misconfiguration.

If you meant for this container to not remain running, please
set the Docker provider configuration "remains_running" to "false":

  config.vm.provider "docker" do |d|
    d.remains_running = false
  end

I do not see the hello world print out.

I have found a lot of Q&A regarding this error, but nothing I could use to resolve my problem.

What am I doing wrong?

Upvotes: 5

Views: 2653

Answers (3)

guy mograbi
guy mograbi

Reputation: 28608

So I have been missing something very fundamental.

The solution I found is easy. Use this amazing contribution from:

https://github.com/bubenkoff/vagrant-docker-example

The important thing to note here is the Dockerfile - which defines the SSH connection in the container. Without the SSH it simply won't work.

However - another tacit fact people are missing - docker uses a socket which requires permissions, however runnign sudo vagrant up --provider will not resolve the issue. in order to run without problems you should sudo su - first OR add your user to docker group.

Read more about that: https://askubuntu.com/questions/477551/how-can-i-use-docker-without-sudo

So to summarize:

  • clone the git repository
  • sudo su -
  • vagrant up --provider docker

==> machine is up and running perfectly..

Upvotes: 3

Hy L
Hy L

Reputation: 582

config.vm.provider :docker do |docker, override|
    docker.image = 'ubuntu'

end

You are missing a command here, you need a command to run the container. something like this:

config.vm.provider :docker do |docker, override|
    docker.image = 'ubuntu'
    docker.run = 'ubuntu'
end

Or you can do something like this:

config.vm.provider :docker do |docker, override|
    docker.image = 'ubuntu'
    docker.cmd = ["/usr/sbin/sshd", "-D"]
end

Upvotes: 1

Evgeny Chernyavskiy
Evgeny Chernyavskiy

Reputation: 2634

Been experiencing the same and following https://github.com/mitchellh/vagrant/issues/3951 for a while. Looks like this is to be fixed by the upcoming minor release of Vagrant.

Upvotes: 1

Related Questions