【手順系】【go】仮想環境上でのWebアプリケーション開発①:go環境構築
本noteの概要
仮想環境上でgolang(以下、go)を使用するため、Vagrantfileやシェルスクリプトに必要なコードを記載し、vagrant upコマンドで仮想環境を構築をする。
本noteの対象者
・VirtualBoxおよびVagrantをインストールしている方
・docker hubのアカウントを開設している方
・以下2つのVagrantプラグインをインストールしている方
❶ vagrant-vbguest
❷ vagrant-docker-compose
(base) $ vagrant plugin list vagrant-docker-compose (1.5.1, global) vagrant-vbguest (0.21.0, global)
(base) $ vagrant plugin install vagrant-vbguest Installing the 'vagrant-vbguest' plugin. This can take a few minutes... Fetching: micromachine-3.0.0.gem (100%) Fetching: vagrant-vbguest-0.21.0.gem (100%) Installed the plugin 'vagrant-vbguest (0.21.0)'! (base) $ vagrant plugin install vagrant-docker-compose Installing the 'vagrant-docker-compose' plugin. This can take a few minutes... Fetching: vagrant-docker-compose-1.5.1.gem (100%) Installed the plugin 'vagrant-docker-compose (1.5.1)'!
本noteの環境
PC環境(ホスト)
# OSのバージョン (base) $ sw_vers ProductName: Mac OS X ProductVersion: 10.14.6 BuildVersion: 18G2022 # Virtualboxのバージョン (base) $ VBoxManage -v 6.1.2r135662 # vagrantのバージョン (base) $ vagrant -v Vagrant 2.2.7
仮想環境(ゲスト)
# Linuxのバージョンが記載されているファイルを検索 vagrant@:~$ ls /etc/*-release /etc/lsb-release /etc/os-release # ゲストOSのバージョンを出力 vagrant@:~$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
This is My note
完成図
<最終的なディレクトリ構成>
▼ ホスト(ローカル)
- src # 今後のアプリケーション開発でソースを管理するための空のディレクトリ - setup - install.sh -Vagrantfile
▼ ゲスト(仮想環境)
- workspace - src - test - lesson.go
1.仮想環境構築準備
⑴ Vagrantfileの作成(Vagrant 初期化)
ターミナル上でvagrant init
コマンドを実行し、Vagrantの初期化を行います。初期化が完了すると、コマンドを実行したディレクトリ下に自動でVagrantfileが作成されます。
なお、vagrant init
コマンドの後に記載しているubuntu/trusty64
は、仮想環境上で動作させるOSの名称になりますが、vagrantのHPに多数の選択肢がありますので適宜必要なものをお使いください。
※ただし、選択したOSによっては、後ほど記載するシェルスクリプトの内容も合わせて変更する必要があります。
(base) $ vagrant init ubuntu/trusty64 =>出力 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.
⑵ 構築する仮想環境の内容設定
① Vagrantfileへの記載
<Vagrantfile>
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 8085, host: 8085, auto_correct: true config.vm.synced_folder 'src', '/home/vagrant/workspace/src' config.vm.provision :docker config.vm.provision :docker_compose, compose_version: "1.24.1" config.vm.provision :shell, :path => "setup/install.sh" end
② シェルスクリプトの作成および記載
(base) $ mkdir setup && cd setup (base) $ touch install.sh
<setup/install.sh>
#!/bin/bash sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" install git curl -O https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz tar -xvf go1.9.1.linux-amd64.tar.gz mv go /usr/local rm go1.9.1.linux-amd64.tar.gz touch /home/vagrant/.bash_profile echo "export PATH=$PATH:/usr/local/go/bin" >> /home/vagrant/.bash_profile echo `export GOPATH=/home/vagrant/workspace:$PATH` >> /home/vagrant/.bash_profile export GOPATH=/home/vagrant/workspace mkdir -p "$GOPATH/bin"
2.仮想環境の起動・接続
⑴ 仮想環境の起動
(base) $ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu/trusty64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'ubuntu/trusty64' version '20190514.0.0' is up to date... ==> default: Setting the name of the VM: golang_default_1581772039639_59475 ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... 〜中略〜 default: go/test/const5.go default: go/test/const6.go default: go/test/convT2X.go default: go/test/convert.go default: go/test/convert1.go default: go/test/convert2.go default: go/test/convert3.go default: go/test/convlit.go default: go/test
⑵ 仮想環境への接続
(base) $ vagrant ssh Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Sat Feb 15 13:10:13 UTC 2020 System load: 0.51 Processes: 82 Usage of /: 4.3% of 39.34GB Users logged in: 0 Memory usage: 35% IP address for eth0: 10.0.2.15 Swap usage: 0% IP address for docker0: 172.17.0.1 Graph this data and manage this system at: https://landscape.canonical.com/ New release '16.04.6 LTS' available. Run 'do-release-upgrade' to upgrade to it.
⑶ インストール内容の確認
① goのインストール確認
vagrant@vagrant-ubuntu-trusty-64:~$ go version go version go1.9.1 linux/amd64
② dockerのインストール確認
vagrant@vagrant-ubuntu-trusty-64:~$ docker version Client: Version: 18.06.3-ce API version: 1.38 Go version: go1.10.3 Git commit: d7080c1 Built: Wed Feb 20 02:27:13 2019 OS/Arch: linux/amd64 Experimental: false Server: Engine: Version: 18.06.3-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.3 Git commit: d7080c1 Built: Wed Feb 20 02:25:38 2019 OS/Arch: linux/amd64 Experimental: false
③ docker-composeのインストール確認
vagrant@vagrant-ubuntu-trusty-64:~$ docker-compose version docker-compose version 1.24.1, build 4667896b docker-py version: 3.7.3 CPython version: 3.6.8 OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018
3.Hello world! (goファイルの作成と実行)
⑴ goファイルの作成
vagrant@vagrant-ubuntu-trusty-64:~$ cd workspace/src vagrant@vagrant-ubuntu-trusty-64:~/workspace/src$ mkdir test && cd test vagrant@vagrant-ubuntu-trusty-64:~/workspace/src/test$ touch lesson.go
⑵ Hello world!(goファイルの実行)
① goファイルの編集
<test/hello.go>
package main import "fmt" func main(){ fmt.Println("Hello world!") }
② goファイルの実行
ⅰ)go buildでコンパイル後に、バイナリファイルを実行
go buildでコンパイルすると、goファイルと同名の実行ファイル(バイナリファイル)が作成される。それを踏まえて、「./実行ファイル名」 でgoファイルを実行。
vagrant@vagrant-ubuntu-trusty-64:~/workspace/src/test$ go build vagrant@vagrant-ubuntu-trusty-64:~/workspace/src/test$ ./hello Hello world!
ⅱ)go runで自動的にコンパイル&バイナリファイルの実行
go runを使用すると自動的にコンパイルおよびバイナリファイルの実行をしてくれる。ただし、go buildのようにカレントディレクトリ以下の全ファイルを読み込むわけではないため注意。
vagrant@vagrant-ubuntu-trusty-64:~/workspace/src/test$ go run hello.go Hello world!
【参考】
・ゲスト OS のタイプが正しく設定されていることを確認する
・fmt - The Go Programming Language
・go run と go buildの違い - PONCOTSU