DEV Community


Posted on • Updated on


How to migrate from docker-compose to vagrant?

Consider you are building multiple containers with networking using docker-compose and for some reason you want to do the same thing in vagrant, how can it be done?

Here is a example:
First the working docker-compose setup. In this example we will build docker containers for mysql and phpmyadmin. phpmyadmin will be connected to the mysql container instance with in the same network.

version: '3.7'
    image: mysql:5.7.36
    restart: always
      MYSQL_DATABASE: test_db
      - "3308:3306"
    image: phpmyadmin/phpmyadmin:latest
    restart: always
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: root
      - "8080:80"
Enter fullscreen mode Exit fullscreen mode

Lets do the same thing in vagrant.

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

# This file works exactly like the docker-compose.yml file.
Vagrant.configure("2") do |config| :private_network, type: "dhcp"
    config.vm.define "mysql" do |db|
        # Without prot forwardign vagrant entwork doesnt work "forwarded_port", guest: 3306, host: 3306
        db.vm.hostname = "mysqldb"
        db.vm.provider "docker" do |d|
          d.image = "mysql:5.7.36"
          d.env = {
            :MYSQL_ROOT_PASSWORD  => "root",
            :MYSQL_DATBASE        => "test_db"
          d.remains_running = "true"
    config.vm.define "phpmyadmin" do |pa| "forwarded_port", guest: 80, host: 8080
        pa.vm.hostname = "phpmyadmin"
        pa.vm.provider "docker" do |d|
            d.image = "phpmyadmin/phpmyadmin:latest"
            d.env = {
                :PMA_HOST       => "mysqldb",
                :PMA_USER       => "root",
                :PMA_PASSWORD   => "root",
                # Without specifying the specific port phpmyadmin container doesn't work
                :PMA_PORT       => "3306", 
            d.remains_running = "true"
Enter fullscreen mode Exit fullscreen mode

Unlike docker-compose it is not possible to set a direct dependency in vagrant. But it is possible to do a sequential build by setting the option --no-parallel eg:- vagrant up --no-parallel.

Even though docker-compose has the dependency set, the dependency is only to the extent of waiting for the dependent container to be built but not waiting for the services in the container to be up and running.

Hence even after building sequentially in vagrant, the dependent services failed to connect. It did not matter vagrant or docker-compose. Services with in the containers need to wait for their dependent services to be up.

So I added a delay in the dependent containers in a timed loop, the dependent service will attempt to connect and upon failure will wait for 30 seconds to connect again, after 10 attempts it gives up.

Oldest comments (1)

abbazs profile image

I've first asked the question in stackoverflow and later answered my own question. This question has been voted to be closed in need of clarity :((

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git