Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Docker Compose to provision Vagrant directly?

Tags:

docker

vagrant

I have a Vagrant file that only brings up a base Ubuntu server and then relies on a shell script to do the provisioning. And this shell script is only about installing Docker and Docker Compose to setup the various containers I have.

Does it make sense to use a shell script to do this? Or is there a way to tell Vagrant to provision directly with Docker Compose? I don't know how different that would be from the already existing Docker provisioner in Vagrant.

like image 861
Bastian Avatar asked Mar 26 '15 10:03

Bastian


People also ask

Can you run Vagrant in Docker?

Vagrant comes with support out of the box for using Docker as a provider. This allows for your development environments to be backed by Docker containers rather than virtual machines. Additionally, it provides for a good workflow for developing Dockerfiles.

What can Docker compose be used for?

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Do you need Vagrant with Docker?

The important difference between Vagrant vs. Docker is that Docker is used to create and run Linux containers, while Vagrant does the work to provision a machine with an operating system, a Docker installation and any other application that needs to run on the OS.


Video Answer


2 Answers

I recently raised the same question, and I created a vagrant provisioner plugin that installs docker-compose and brings up docker using it. To use it:

  1. Install the plugin: vagrant plugin install vagrant-docker-compose
  2. Add the following lines to your Vagrantfile.

    config.vm.provision :docker
    config.vm.provision :docker_compose, yml: "/vagrant/docker-compose.yml", rebuild: true, run: "always"
    

For a full example see https://github.com/leighmcculloch/vagrant-docker-compose

like image 106
Leigh McCulloch Avatar answered Oct 11 '22 12:10

Leigh McCulloch


It appears that as of vagrant 1.7.2 there is not direct support via the Docker Provisioner to do this type of operation. The provider did not have any mention of it either. Vagrant docs: Docker provisioning, Vagrant docs: Docker commands

My guess is the Vagrant maintainers as of this version feel the Vagrantfile when used with the Docker provider offers similar behavior. This may explain why they did not venture to add direct support for Fig before it was replaced with Docker Compose.

I have found that using Docker with Vagrant requires you to find the right mix of using each tool that you find optimal for yourself. For example, You may find using Dockerfiles and Docker commands like docker compose more intuitive than trying to implement that logic into the Vagrantfile. Alternatively, you could try a hybrid of Dockerfiles and using Vagrant to reference the Dockerfiles which gives you the Docker logic in the Dockerfile and the build "orchestration" using the normal Vagrantfile.

Here is how that might look:

  1. Use very simple Vagrant Docker provider config, rely on Dockerfile for everything else

    Use d.build_dir = "." to reference a Dockerfile

  2. Use Docker provisioner in Vagrant to start everything

    vagrant up --provision-with docker
    
like image 21
Padge Avatar answered Oct 11 '22 14:10

Padge