備忘メモ(Vagrant + VirtualBox + Docker + VisualStudioCode on Mac)
そのうち手順を整理することになりそうなのでメモ。
参考
Vagrantを使う「Mac最速のDocker環境」を初心者向けに解説【遅いMac for Dockerを卒業】 - Qiita
Vagrant+VirtualBoxでUbuntu環境構築 - Qiita
Vagrantバージョン
Big Surだとバージョン2.2.14はvagrant upがコケるので、2.2.13に変更。 コケない場合は作業不要。
ダウングレードは以下参考。二行目のversionと三行目のsha256 だけ変更。
Downgrade homebrew (cask) package · GitHub
$ brew edit vagrant --cask
cask "vagrant" do
version "2.2.13"
sha256 "b65adb59bef5b69be61bd79a34e3532533a172491b307718d1c4a5a46209e81f"
url "https://releases.hashicorp.com/vagrant/#{version}/vagrant_#{version}_x86_64.dmg",
verified: "hashicorp.com/vagrant/"
appcast "https://github.com/hashicorp/vagrant/releases.atom"
name "Vagrant"
desc "Development environment"
homepage "https://www.vagrantup.com/"
pkg "vagrant.pkg"
uninstall script: {
executable: "uninstall.tool",
input: ["Yes"],
sudo: true,
},
pkgutil: "com.vagrant.vagrant"
zap trash: "~/.vagrant.d"
end
作業の流れ
- Homebrewインストール
- VirtualBoxインストール
- Vagrantインストール
- Vagrantプラグインインストール
- Vagrant設定
- Mutagenインストール
- Mutagen設定
- docker-composer作成
- 確認用PHP作成
- ssh config作成(無い場合)
- Vagrant起動
- Vagrant接続(terminal)
- Docker起動
- アクセス確認
- Docker停止
- Vagrant切断
- Vagrant接続(Visual Studion Code)
- Vagrant停止
Homebrewインストール
これまんまで。macOS(またはLinux)用パッケージマネージャー — Homebrew
VirtualBoxインストール
$ brew install --cask virtualbox
セキュリティとプライバシーの設定でコケたら設定してやり直し。 設定したら再起動をする必要があるので注意。
Vagrantインストール
$ brew install --cask vagrant
Vagrantプラグインインストール
$ vagrant plugin install vagrant-disksize vagrant-hostmanager vagrant-mutagen vagrant-docker-compose
Vagrant設定
Box調査。Discover Vagrant Boxes - Vagrant Cloud 今回はubuntu/xenial64を選択。
作業フォルダ作成&移動。以後はcdしない限りここにずっと居て、各種ディレクトリ、ファイルもここに作成。 おっと、Macだとフォルダだったかな。
$ mkdir ~/Documents/Vagrant
$ cd ~/Documents/Vagrant
Vagrantfile作成。
$ vagrant init ubuntu/xenial64
編集
$ vi Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.hostname = "vagrant-ubuntu"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.cpu = 1
vb.memory = "2048"
vb.customize ['modifyvm', :id, '--natdnsproxy1', 'off']
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'off']
end
config.disksize.size = '10GB'
config.mutagen.orchestrate = true
config.vm.synced_folder './', '/home/vagrant/app', type: "rsync",
rsync_auto: true,
rsync__exclude: ['.git/', 'node_modules/', 'log/', 'tmp/']
config.vm.provision :docker, run: 'always'
config.vm.provision :docker_compose
end
Mutagenインストール
brew install mutagen-io/mutagen/mutagen
Mutagen設定
vi mutagen.yml
sync:
app:
mode: "two-way-resolved"
alpha: "./"
beta: "vagrant-ubuntu:/home/vagrant/app"
ignore:
vcs: true
paths:
- "/node_modules"
- "/log"
- "/tmp"
docker-composer作成
$ vi docker-composer.yml
version: '3'
services:
web:
image: php:7.4-apache-buster
ports:
- "80:80"
volumes:
- ./public:/var/www/html:cached
確認用PHP作成
$ mkdir public
$ vi public/index.php
<?php
phpinfo();
$ vi public/.htaccess
DirectoryIndex index.php
$ chmod -R 755 public
ssh config作成(無い場合)
まっさらな環境の場合、ファイルが無いので作る。中身は空でOK。
$ mkdir ~/.ssh
$ touch ~/.ssh/config
Vagrant起動
$ vagrant up
Vagrant接続(terminal)
$ vagrant ssh
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-198-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
1 package can be updated.
1 of these updates is a security update.
To see these additional updates run: apt list --upgradable
New release '18.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Docker起動
vagrant@vagrant-ubuntu:~$ cd app
vagrant@vagrant-ubuntu:~/app$ docker-compose up
アクセス確認
ブラウザで http://192.168.33.10/ にアクセス。 この場合のIPアドレスはVagrantfileで指定したもの。
Docker停止
Ctrl-C
Vagrant切断
vagrant@vagrant-ubuntu:~$ exit
Vagrant接続(Visual Studio Code)
Visual Studio Codeにremote developmentを追加。 ssh configにvagrant用のエントリ追加。
$ vagrant ssh-config >> ~/.ssh/config
後は繋ぐのみ。 Macの場合はシステム環境設定のセキュリティとプライバシーでVisual Studio Codeにフォルダのアクセス権を設定してあげる必要があるかも。
Vagrant停止
$ vagrant halt