Go×Dockerで環境構築を実施する。

### 1. Go×Dockerで環境構築を実施する。
Go×Dockerは最近のナウイ開発ではよくこの構成になっています。本記事では`Go×Docker`の環境構築の方法を記載しております。
![スクリーンショット 2022-06-10 17.54.22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/86ccabbe-c982-23aa-2505-dbe9e13c16f9.png)

### ①go moduleを作成する
go modはGoのアプリケーションルートのようなものです。値はなんでも良いのですが、`github.com/ユーザ名/アプリケーション名`が慣例となっています。
```shell:go module
~/develop/go_docker $ go mod init github.com/kouji0705/go_docker
go: creating new go.mod: module github.com/kouji0705/go_docker
```

### ②アプリケーションフレームワークである`echo`をインストールする。
```shell:
~/develop/go_docker $ go get -u github.com/labstack/echo        
go get: added github.com/labstack/echo v3.3.10+incompatible
go get: added github.com/labstack/gommon v0.3.1
go get: added github.com/mattn/go-colorable v0.1.12
go get: added github.com/mattn/go-isatty v0.0.14
go get: added github.com/valyala/bytebufferpool v1.0.0
go get: added github.com/valyala/fasttemplate v1.2.1
go get: added golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
go get: added golang.org/x/net v0.0.0-20220607020251-c690dde0001d
go get: added golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68
go get: added golang.org/x/text v0.3.7
```

### ③main.goを作成する。
```go:main.go
package main

import (
	"net/http"

	"github.com/labstack/echo"
)

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "HelloWorld!")
	})
	e.Logger.Fatal(e.Start(":1323"))
}
```

### ④ホットリロードするairをインストールする。
```go:
~/develop/go_docker $ go install github.com/cosmtrek/air@latest
~/develop/go_docker $ air init

  __    _   ___  
 / /\  | | | |_) 
/_/--\ |_| |_| \_ , built with Go 

.air.toml file created to the current directory with the default settings
```

### ⑤Dockerfile、docker-compose.ymlを作成する。

```Dockerfile:Dockerfile
# Dockerfile
FROM golang:1.17

ENV TZ /usr/share/zoneinfo/Asia/Tokyo

ENV ROOT=/go/src/app
WORKDIR ${ROOT}

ENV GO111MODULE=on

COPY . .
EXPOSE 1323

RUN go install github.com/cosmtrek/air@latest
CMD ["air"]
```

```yml:docker-compose.yml
version: '3'
services:
  app:
    build: .
    volumes:
      - ./:/go/src/app
    ports:
      - "1323:1323"
```

### Dockerを実行する

```go:アプリ実行
~/develop/go_docker $ docker-compose up
Creating network "go_docker_default" with the default driver
Building app
[+] Building 30.0s (9/9) FINISHED                                                                         
 => [internal] load build definition from Dockerfile                                                 0.0s
 => => transferring dockerfile: 238B                                                                 0.0s
 => [internal] load .dockerignore                                                                    0.0s
 => => transferring context: 2B                                                                      0.0s
 => [internal] load metadata for docker.io/library/golang:1.17                                       3.0s
 => [1/4] FROM docker.io/library/golang:1.17@sha256:b1ff3263f543b4c458e172f3df429a3e7dfed29d4359ab  20.9s
 => => resolve docker.io/library/golang:1.17@sha256:b1ff3263f543b4c458e172f3df429a3e7dfed29d4359ab8  0.0s
 => => sha256:dceb25b534d8da2aa50da5c6489fc4120db24f24dcf8b3b1862d1a8cebf1210b 1.80kB / 1.80kB       0.0s
 => => sha256:0e970dee9aa41b53e9096c0fe39d64d8e4e96dfbffaa7b47a9d1897ae965b9b0 7.11kB / 7.11kB       0.0s
 => => sha256:bbec5306b106b1062838a5fa65c34731772b8da7ff56a9aec828890a5bf1f226 156B / 156B           0.6s
 => => sha256:b1ff3263f543b4c458e172f3df429a3e7dfed29d4359ab839a614f903252e1df 2.35kB / 2.35kB       0.0s
 => => sha256:ad491e6a2878e96d876127fb0107f5063e90c343528b58151170cd95ce9b0a95 134.96MB / 134.96MB  12.4s
 => => extracting sha256:ad491e6a2878e96d876127fb0107f5063e90c343528b58151170cd95ce9b0a95            8.1s
 => => extracting sha256:bbec5306b106b1062838a5fa65c34731772b8da7ff56a9aec828890a5bf1f226            0.0s
 => [internal] load build context                                                                    0.3s
 => => transferring context: 6.60MB                                                                  0.2s
 => [2/4] WORKDIR /go/src/app                                                                        1.3s
 => [3/4] COPY . .                                                                                   0.0s
 => [4/4] RUN go install github.com/cosmtrek/air@latest                                              4.1s
 => exporting to image                                                                               0.4s
 => => exporting layers                                                                              0.4s
 => => writing image sha256:8800bf76c4f662e1931d2234a5039b9ab28ed8e9b758a991b27ccc0e0b66cf6d         0.0s
 => => naming to docker.io/library/go_docker_app                                                     0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating go_docker_app_1 ... done
Attaching to go_docker_app_1
app_1  | 
app_1  |   __    _   ___  
app_1  |  / /\  | | | |_) 
app_1  | /_/--\ |_| |_| \_ , built with Go 
app_1  | 
app_1  | watching .
app_1  | !exclude tmp
app_1  | building...
app_1  | go: downloading github.com/labstack/echo v3.3.10+incompatible
app_1  | go: downloading github.com/labstack/gommon v0.3.1
app_1  | go: downloading golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
app_1  | go: downloading github.com/mattn/go-isatty v0.0.14
app_1  | go: downloading github.com/mattn/go-colorable v0.1.12
app_1  | go: downloading github.com/valyala/fasttemplate v1.2.1
app_1  | go: downloading golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68
app_1  | go: downloading github.com/valyala/bytebufferpool v1.0.0
app_1  | go: downloading golang.org/x/net v0.0.0-20220607020251-c690dde0001d
app_1  | go: downloading golang.org/x/text v0.3.7
app_1  | running...
app_1  | 
app_1  |    ____    __
app_1  |   / __/___/ /  ___
app_1  |  / _// __/ _ \/ _ \
app_1  | /___/\__/_//_/\___/ v3.3.10-dev
app_1  | High performance, minimalist Go web framework
app_1  | https://echo.labstack.com
app_1  | ____________________________________O/_______
app_1  |                                     O\
app_1  | ⇨ http server started on [::]:1323
```
→ブラウザからアクセスすると、「HelloWorld」が表示されているのが確認できます！！
![スクリーンショット 2022-06-10 18.12.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/9e8be255-5278-8342-6e6d-4d00f81189c6.png)