Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

abbazs
abbazs

Posted on • Edited 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 formysql andphpmyadmin.phpmyadmin will be connected to themysql 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 modeExit fullscreen mode

Lets do the same thing invagrant.

# -*- 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    endend
Enter fullscreen modeExit fullscreen mode

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

Even thoughdocker-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 invagrant, the dependent services failed to connect. It did not mattervagrant ordocker-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.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
abbazs profile image
abbazs
A software generalist.
  • Location
    Chennai
  • Joined

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A software generalist.
  • Location
    Chennai
  • Joined

More fromabbazs

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp