DEV Community

abbazs
abbazs

Posted on • Edited on

1

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'
services:
  db:
    image: mysql:5.7.36
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
    ports:
      - "3308:3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    restart: always
    environment:
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: root
    ports:
      - "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|
    config.vm.network :private_network, type: "dhcp"
    config.vm.define "mysql" do |db|
        # Without prot forwardign vagrant entwork doesnt work
        db.vm.network "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"
        end
      end
    config.vm.define "phpmyadmin" do |pa|
        pa.vm.network "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"
        end
    end
end
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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
abbazs profile image
abbazs

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 :((

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay