強欲で謙虚なツボツボ

趣味の読書の書の方

備忘録(Go + Air in Docker)

dockerコンテナで go run main.go をした後に、ホスト側のローカルで修正しても go run し直すかdockerコンテナを再起動するしないと反映されない。
Airを使うと変更を随時反映させることができる。(ホットリロード・live reload)
Realizeというのもあるが使ったことはない。

 

Dockerfile

GitHub - cosmtrek/air: ☁️ Live reload for Go apps

FROM golang:1.16-alpine
RUN apk update && apk add git
RUN go get github.com/cosmtrek/air@latest
WORKDIR /usr/src/app
COPY go.mod go.sum .
RUN go mod download && go mod verify
COPY . .
EXPOSE 8080

 

docker-compose.yml

コマンドを go run main.go ではなく air -c .air.toml としてairで起動させる。
.air.tomlは設定ファイルで下記を編集。

air/air_example.toml at master · cosmtrek/air · GitHub

version: "3"

services:
  go:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: go
    volumes:
      - ./:./
    ports:
      - 8080:8080
    tty: true
    command: /bin/sh -c "air -c .air.toml"

あとは、dockerコンテナを起動すれば編集を随時反映してくれる。