How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant

Introduction

I was doing team development using Docker for Mac, but since it is slow, let's stop Docker for Mac and start a virtual environment and create an explosive Docker environment! !! I came to.

Why Docker for Mac is slow

On Linux, the VFS is shared between the host and the container, so the reflection without overhead is guaranteed. However, on macOS (and other non-Linux platforms), there is significant overhead for full consistency ... Docker Japanese document and other Qiita articles also said that, but I don't really understand ... For the time being, the Docker for Mac file sharing system seems to mount slowly on osxfs, The main reason is that the FSEvents API of OSX and the Linux ’s inotify API are mapped. Slow cause

What is the difference between Docker for Mac and the environment (Virtual Box + Docker) to be built this time? Abandon Docker for Mac, which significantly reduces DX, and get the fastest Docker environment for Mac The above article was extremely easy to understand! !! The figure is helpful (; ∀ ;)

Operating environment

PC specs

Mac OS Catalina 10.15.7

Docker version

20.10.0-beta1

Vagrant version

Vagrant 2.2.13

Ubuntu version

Ubuntu 18.04.5 LTS

Overview

① Install VirtualBox and Vagrant

② Install vagrant plugin for Docker and mutagen (described later)

③ Create vagrant file

④ File synchronization with mutagen

⑤ Start Docker

I can see many steps, but let's do our best ... (laughs)

① Install VirtualBox and Vagrant

Use Homebrew to install the necessary tools. Homebrew installation

Install VirtualBox
brew cask install virtualbox
Install Vagrant
brew cask install vagrant
Download Vagrant Box

Download Vagrant Box (OS). (It took quite a while)

vagrant box add ubuntu/bionic64 

After the download is complete, you can check it with the following command

vagrant box list
ubuntu/bionic64 (virtualbox, 20200229.0.0)

② Install vagrant plugin for Docker and mutagen (described later)

Install the required Vagrant Plugin. We will discuss mutagen later.

vagrant plugin install vagrant-disksize vagrant-hostsupdater vagrant-mutagen vagrant-docker-compose

③ Create vagrant file

Vagrant init in the development directory

cd ~/my_app       (Move to development directory)
vagrant init ubuntu/bionic64      (Initialize with vagrant)

The following sentence will appear, so you can create a vagrantfile!

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

We will edit the Vagrantfile! !! !! Edit the contents of the Vagrant file as follows! config.vm.hostname is OK in the development directory! If you don't understand anything else Vagrant command and Vagrantfile settings memo It was easy to understand if you look at the "vagrantfile settings" in the above article. Thank you(:_;)

~/my_app/Vagrantfile


Vagrant.configure('2') do |config|
  config.vm.box = 'ubuntu/bionic64'

  config.vm.hostname = 'my_app'

  config.vm.network :private_network, ip: '192.168.50.10'
  
  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.provider :virtualbox do |vb|
    vb.gui = false
    vb.cpus = 4
    vb.memory = 8192
    vb.customize ['modifyvm', :id, '--natdnsproxy1', 'off']
    vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'off']
  end

  config.disksize.size = '30GB'
  config.mutagen.orchestrate = true

  config.vm.synced_folder './', '/home/vagrant/app', type: "rsync",
                                                                                                              
  rsync_auto: true,
                                                                                                              
  rsync__exclude: ['.git/', 'node_modules/', 'log/', 'tmp/', 'vendor']

  config.vm.provision :docker, run: 'always'
  config.vm.provision :docker_compose
end

If the host side path of config.vm.synced_folder is set to "./ ", it will be easier because there is no need to rewrite the path for each work environment when sharing work.

This completes the preparation of Vagrant + VirtualBox.

④ File synchronization with mutagen

Synchronize the edited file on Mac and Linux. This is achieved with a tool called Mutagen. Install mutagen with the following command. (This also took time)

brew install mutagen-io/mutagen/mutagen

After installation, create a mutagen configuration file mutagen.yml. Create it in the same directory as Vagrantfile.

~/my_app


touch mutagen.yml

The contents are as follows.

~/my_app/mutagen.yml


sync:
  app:
    mode: "two-way-resolved"
    alpha: "./"
    beta: "my-app:/home/vagrant/app"
    ignore:
      vcs: true
      paths:
        - "/node_modules"
        - "/log"
        - "/tmp"

Then start the VM with $ vagrant up and Mutagen will do a two-way file sync. This synchronization is bi-directional, near real-time, and is achieved by transferring files rather than mounting the filesystem, so there is almost no overhead when finally mounted in a Docker container.

⑤ Start Docker

Launch Vagrant

An error occurred after executing the command ,,,,,

~my_app


vagrant up
/Users/hoge/.vagrant.d/gems/2.4.9/gems/vagrant-mutagen-0.1.2/lib/vagrant-mutagen/Mutagen.rb:22:in `initialize': No such file or directory @ rb_sysopen - /Users/hoge/.ssh/config (Errno::ENOENT)

You say you don't have config (sweat) Let's make it! !!

~my_app


touch /Users/hoge/.ssh/config

once again! !!

~my_app


vagrant up

It seems that this time it was done well ε- (´∀ ` *) Hot

SSH connection to virtual environment

vagrant ssh

Then, this time, some kind of update information ...

New release ''20.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

You shouldn't do this.

vagrant box add ubuntu/bionic64 It seems that it is necessary to download the OS that matches the version firmly, so ...

Launch Docker in Vagrant

The following command is an example when docker-compose.yml exists in the project directory.

cd app
docker-compose up

Recommended Posts

How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to build docker environment with Gradle for intelliJ
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
Build a PureScript development environment with Docker
How to build Rails 6 environment with Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
Steps to build a Ruby on Rails development environment with Vagrant
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
Build a development environment for Docker, java, vscode
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
How to build a Pytorch environment on Ubuntu
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Creating a java web application development environment with docker for mac part1
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Create a java web application development environment with docker for mac part2
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
How to build Java development environment with VS Code
How to make Laravel faster with Docker for Mac
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
Build a development environment to create Ruby on Jets + React apps with Docker
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Build a Node.js environment with Docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Build Java development environment (for Mac)
I tried to create a padrino development environment with Docker
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to install Pry after building Rails development environment with Docker
How to install the language used in Ubuntu and how to build the environment
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Laravel development environment construction with Docker (Mac)
Build a Java development environment on Mac
Build a simple Docker + Django development environment
How to build an environment with Docker, which is the minimum required to start a Rails application
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to execute with commands of normal development language in Docker development environment
[Node.js express Docker] How to define Docker environment variables and load them with node.js
Build a Laravel / Docker environment with VSCode devcontainer
Build a simple Docker Compose + Django development environment
[Win10] Build a JSF development environment with NetBeans
How to build API with GraphQL and Rails
Prepare a scraping environment with Docker and Java
A reminder of Docker and development environment construction
Build mecab (NEologd dictionary) environment with Docker (ubuntu)