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.
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.
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.
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.
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:
vagrant plugin install vagrant-docker-compose
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
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:
Use very simple Vagrant Docker provider config, rely on Dockerfile
for everything else
Use d.build_dir = "."
to reference a Dockerfile
Use Docker provisioner in Vagrant to start everything
vagrant up --provision-with docker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With