Access Docker features when using Vagrant - vagrant

I've been using Vagrant for some time and am now exploring Docker. I've always loved Vagrant for its simplicity. Right now, I'm trying to use the Docker provider within Vagrant. I'm using the following Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.image = "fgrehm/vagrant-ubuntu:precise"
end
end
My understanding is that I can just run vagrant up. I can then run the Docker container using vagrant docker-run -- <command>.
So far so good. What makes Docker so awesome is the fact that you can mess around and commit changes. I do not understand is how to incorporate this in my workflow when using the Docker provider for Vagrant. E.g. how do I run docker commit to commit the state of the container? I'd expect some kind of vagrant docker-commit, but this does not exist?
Edit: In hindsight, I think this is not the way you should be using Vagrant/Docker. Though it is claimed that both tools complement each other, my opinion is that they do not (yet) play really well together. Right now, we're using Dockerfiles to build our images. Additionally, we made a set of bash scripts to launch a container.

Try to specify "has_ssh" to true, and ask Vagrant to use port 22 instead of 2222:
ENV["VAGRANT_DEFAULT_PROVIDER"] = "docker"
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d, o|
d.image = "fgrehm/vagrant-ubuntu:precise"
d.has_ssh = true
o.ssh.port = 22
end
end
Then use vagrant up; vagrant ssh to access it.

Another option if you have same client version of docker at your host-host machine as docker server started inside boot2docker VM. In that case you can set
export DOCKER_HOST=tcp://:4243 (or 2375 depending on docker version)
and then access all docker features running local client:
docker ps -a

Related

How to run scripts automatically after doing vagrant ssh?

I am new to Vagrant but good in Docker.
In Vagrant I am aware of the fact that
config.vm.provision :shell,path: "bootstrap.sh", run: 'always'
in the Vagrantfile will provision vagrant box while doing vagrant up. With this, the vagrant box interactive console appears after the intended provisioning is done.
But I need to configure in such a way that, first the control goes in to vagrant box console and then the intended script is up and running. Because my requirement is to run a script automatically post vagrant up and not to run a bootstrapped script.
In analogy with Docker, my question can be seen as
what is the Vagrant equivalent for CMD in Dockerfile ?
You can look at vagrant triggers. You can run dedicated script/command after each specific vagrant command (up, destroy ...)
For example
Vagrant.configure("2") do |config|
# Your existing Vagrant configuration
...
# start apache on the guest after the guest starts
config.trigger.after :up do |trigger|
trigger.run_remote = {inline: "service apache2 start"}
end
end

How to use packer with box file?

I have a vagrantfile using a box on top of virtualbox with a provision script.
Now I am trying to use packer to output a box already after provision.
However I cannot find a builder to use the ".box" file I already have. What am I doing wrong?
I just got a solution to this tiny little problem (convert a vagrant .box file to .ova for use by packer):
Create a vm using the .box file as a base. I use this Vagrantfile, with box opscode-centos-7.0:
$provisioning_script = <<PROVISIONING_SCRIPT
adduser packer
echo "packer" | passwd packer --stdin
echo "packer ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/packer
PROVISIONING_SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = "opscode-centos-7.0"
config.ssh.insert_key = false
config.vm.provider "virtualbox" do |v|
v.name = "packer-base"
end
config.vm.provision :shell, inline: $provisioning_script
end
run vagrant up
run vagrant halt
run vboxmanage export --ovf20 -o packer-base.ova packer-base
run vagrant destroy
This also creates the packer user with a default password so that packer can easily connect to the instance to do stuff. Also note the insert_key parameter that will prevent replacing the vagrant default insecure key with a secure one and allow subsequent vagrant setups to properly connect via SSH to the new images (after packer is done).
Packer out-of-the-box doesn't support using Vagrant boxes as input (yet).
But there is a custom plugin, see this comment.
If you want to build a vagrant box that runs with provider virtualbox, have a look here.
However, it takes an iso or ovf as input, not a vagrant box.
Have a look at these templates to get you started using the virtualbox builder with packer.
Make sure your run the post-processor to convert the virtualbox vm into a vagrant box.

Vagrant workflow

Hi i was trying to understand what exactly is done when running
vagrant up
my reason for that is that in my case we need to install a lot of utilities.
i.e Version control tools, build tools, ide, etc...
which takes a lot of time.
so actually i wanted a 'box' with all those tools.
After i have clean environment and got all tools, i would like to make CI for our product.
If i will reinstall all utilities it should take a lot of time. so what i am actually need is just installing and testing our product.
How should i handle that ??
create my own box? does the command reinstall all utilities when we make CI ??
what i actually need are 2 processes :
1.installing utilities for my vm. (once a month)
2.test our product (each commit\push to version control)
how can i achieve that ?
For the first time, vagrant up will create a new VM for you, pulling the box image if needed, and it will provision it with what you configured in the Vagrantfile. In the provision configuration, you can tell Chef or Puppet to install all the utilities and tools that you need.
When you suspend or halt the VM, the next time you do a vagrant up it will only bring that VM back up. It will not install or try to provision it again.
You can force it with vagrant up --provision or just vagrant provision.
This usually works well in a development environment.
In a CI environment, it may not be possible to have the VM already provision, forcing you to run the provisioning step every time. You can achieve what you need packaging your own box with the tools already installed, essentially creating a golden or base image.
Just be extra careful so that the CI environment don't differ for what you have in production.
All depend on the setting in Vagrantfile
you have modules folder to put all puppet modules, manifests folder with site.pp and Vagrantfile as below under same place.
Give you a sample of Vagrantfile I used mostly.
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box "
config.vm.provision :puppet do |puppet|
puppet.module_path = "modules"
puppet.manifests_path = "manifests"
puppet.manifest_file = "site.pp"
end
config.vm.define :www do |config|
config.vm.host_name = "www.example.com"
config.vm.network :private_network, ip: "192.168.1.2"
end
config.vm.define :db1 do |config|
config.vm.host_name = "db1.example.com"
config.vm.network :private_network, ip: "192.168.1.4"
end
end
So after run vagrant up www or vagrant up db1 it will start the box with puppet apply directly. You can understand this way as puppet masterless
Vagrant just make the box up running from linux image defined in config.vm.box_url as a simple fresh linux box, and mount your local folder to new box's /vagrant folder, then hand over to puppet. How the server to be provisioned, will be depended on site.pp (define applications on each node) and puppet modules. The command is similar as
puppet apply --modulepath /vagrant/module /vagrant/manifests/site.pp
So if your puppet modules are fine, your new box will automatically have all utilities and products installed. Then you run vagrant ssh www or vagrant ssh db1, you can login it and start working.
You can put your local folder with moduels, manifests folders and Vagrantfile to version control (such as git). So developers can clone the git repository to their own computer easily.

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?
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..
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.
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

Vagrant & Docker: The container started never left the "stopped" state

I'm following the vagrant guide to using docker but I receive this error when launching vagrant:
Jons-MacBook-Pro:vagrant jonhaven$ vagrant up --provider=docker
Bringing machine 'default' up with 'docker' provider...
==> default: Docker host is required. One will be created if necessary...
default: Docker host VM is already ready.
==> default: Vagrant has noticed that the synced folder definitions have changed.
==> default: With Docker, these synced folder changes won't take effect until you
==> default: destroy the container and recreate it.
==> default: Starting container...
==> default: Waiting for container to enter "running" state...
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
And this is my Dockerfile (same as in the video):
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.image = "paintedfox/postgresql"
end
end
Has anyone seen this before? I'm on OSX 10.9.4 and using Vagrant works otherwise, just not with Docker.
Edit:
I followed the advice given and verified that I could run my docker image via docker. My working docker command is this:
docker run -p 8888:8888 -d haven/play /opt/activator/activator ui -Dhttp.address=0.0.0.0
However, I can't get this to launch via vagrant no matter what combination of create_args or cmd options I use in Vagrant. To be clear, the issue is not the ports but that the docker container will not stay running.
The Docker will stop if there isn't something keeping standard out going.
It looks like the paintedfox/postgresql CMD is ["/sbin/my_init"]
I assume this is a non-daemonized command that is meant to keep the container running which would mean it's exiting by error. I would try to debug by running the docker manually:
docker run -i -t paintedfox/postgresql /bin/bash
and then try to run the command:
/sbin/my_init
and see if it exits with an error. If you are running the docker in a vagrant you will first have to SSH into the Vagrant with
vagrant ssh

Resources