Go+GinをDocker上で動かす(Airでホットリロード)

  • 2021.07.31
  • Go
Go+GinをDocker上で動かす(Airでホットリロード)

やりたいこと

  • Docker上でginを動かす
  • ソースの変更を検知して自動ビルドする(ホットリロード)

やったこと

最終的なディレクトリ構成

.
├── Dockerfile
├── docker-compose.yml
└── src
    ├── .air.toml
    ├── go.mod
    ├── go.sum
    └── main.go

Go+GinをMacで動かす

最低限の実装で、まずはMac上で動かします。Goのインストールや、モジュールモードでの開発についてはここでは解説しません。
以下のmain.goをsrcディレクトリ以下に配置し、src配下で go run main.go を実行すると、8080ポートでローカルサーバが起動します。
(MacにGoの環境がない方は、そのまま次に進んでもらってもOKです。)

main.go

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "Hello World",
        })
    })
    router.Run()
}

Docker上で動かす

以下のように、Dockerfile、docker-compose.ymlを準備します。

Dockerfile

FROM golang:latest
WORKDIR /go/src
COPY ./src .
CMD ["go", "run", "main.go" ]

docker-compose.yml

version: '3'
services: 
  go:
    build: 
      context: .
    volumes:
      - ./src:/go/src
    ports: 
      - 8080:8080

docker compose up コマンドを打つと、docker上でサーバが起動し、localhost:8080でアクセスできます。

ホットリロードの機能を入れる

Goのホットリロードのライブラリとして、Airを入れます。
Dockerfileの編集と、設定ファイルの作成が必要です。

FROM golang:latest
WORKDIR /go/src
COPY ./src .
# 以下追記
RUN go get -u github.com/cosmtrek/air
# 以下編集
CMD ["air", "-c", ".air.toml"]

以下は公式のサイトから取ってこれます。
https://github.com/cosmtrek/air/blob/master/air_example.toml

.air.toml

# Config file for [Air](https://github.com/cosmtrek/air) in TOML format

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# Exclude unchanged files.
exclude_unchanged = true
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms

[log]
# Show log time
time = false

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = true

ここまでできたら、 docker compose up --build で再度Dockerを立ち上げます。
※Dockerfileを編集しているので、–buildオプションをつけてビルドし直す必要があります。
この状態で、main.goの一部を編集して上書きすると、コンソール上に以下が表示され、自動でコンパイルし直してくれます。

go_1  | main.go has changed
go_1  | building...
go_1  | running...