1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vagrant環境に開発環境を引っ越し

Posted at

開発環境

docker for mac 環境で動作の遅さを感じるので、
今更ながらvagrantで開発環境を作ってみようと思いました。

やってみてdockerの動作が早くなってびっくりです。
自分の場合だと、vscodeのおかげで開発に支障がでないので、
結果的にやってよかったと思います。

遅いと悩んでいるのであれば、試してみると快適になるかもしれません。

vagrant 環境設定

$ vagrant init 
$ mkdir source

Vagrantfile の中身

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "bento/ubuntu-20.04"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./source", "/home/vagrant/shareMacSource"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "8192"
    vb.cpus = 4
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

vagrant で 8080 とかで動かす時、この設定をよしなに変更することで、
mac でも localhost:8080 で接続することができる。

  # config.vm.network "forwarded_port", guest: 80, host: 8080

vagrant ssh-config を実行して ssh/config に追記する内容を出力する

Host develop
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile < youre privater ssh-key path>
  IdentitiesOnly yes
  LogLevel FATAL

vagrant に ssh で接続

$ ssh develop
---
vagrant@vagrant:~$ sudo passwd vagrant
new password:
Retype new password:

※入力したパスワードは非表示

nano から vim に変更

$ sudo update-alternatives --set editor /usr/bin/vim.basic

Docker インストール

公式ドキュメントのコマンドをコピペする
Ubuntu 20.04へのDockerのインストールおよび使用方法 | DigitalOcean


$ sudo apt-get update
$ sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

$ sudo apt update
$ apt-cache policy docker-ce

$ sudo apt install -y docker-ce

$ sudo systemctl status docker

$ sudo usermod -aG docker ${USER}

-- ここでsshを切断し、再度接続 --

$ id -nG
vagrant@vagrant:~$ id -nG
vagrant adm cdrom sudo dip plugdev lxd lpadmin sambashare docker

-- docker が追加されている ---

docker コマンドが実行できるか確認
$ docker run hello-world

ログインシェルを fish に変更

$ sudo apt-add-repository ppa:fish-shell/release-3
$ sudo apt-get update
$ sudo apt-get install -y fish



$ which fish
> /usr/bin/fish 

$ sudo vi /etc/shells
※ fish のパスが追加されていることを確認(追加されていない場合は追記)

デフォルトシェルの変更
$ chsh -s /usr/bin/fish 
Password:


fish プラグインをインストール

$ fisher install jethrokuan/z
$ fisher install jethrokuan/fzf
$ fisher install decors/fish-ghq

便利コマンドを追加

ディレクトリ構造

.config/fish
├── config.fish
└── functions
    ├── peco_change_history.fish
    └── peco_exploer_directory.fish
config.fish
### ショートカットを追加
# peco shortcut key
function fish_user_key_bindings

  # Bind for peco show history to Ctlr+h
	bind \ch peco_change_history

  # Bind for prco change directory to Ctrl+f
	bind \cf peco_exploer_directory

end

pecho_change_history.fish
function peco_change_history
  if test (count $argv) = 0
    set peco_flags --layout=bottom-up
  else
    set peco_flags --layout=bottom-up --query "$argv"
  end
history|peco $peco_flags|read foo
if [ $foo ]
    commandline $foo
  else
    commandline ''
  end
end

peco_exploer_directory.fish
function peco_exploer_directory
   set -l query (commandline)

  if test -n $query
    set peco_flags --query "$query"
  end

  z -l | peco $peco_flags | awk '{ print $2 }' | read recent
  if [ $recent ]
      cd $recent
      commandline -r ''
      commandline -f repaint
  end
end

ホームディレクトリで vagrant コマンドが実行できるようにする

※ vagrant を使わない想定なのでこれでOK
開発環境は develop に集約してそこで docker とか動かしたい


$ direnv edit .
export VAGRANT_CWD=<Vagrantfile があるディレクトリを指定>

# 環境変数の有効化
$ direnv allow 

# vagrant コマンドを指定し、path指定したvagrant を操作できればOK
$ vagrant status 
Current machine states:
default                   running (virtualbox)

まとめ

docker for mac が遅くネイティブレベルの早さを手に入れたいと思い、
思い切って vagrant に開発環境を作ってみました。
macしかできないことは普段やらないので、メインPCをLinuxにするか悩み中です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?