热重载在使用 golang 的 docker 中不起作用 (github.com/cosmtrek/air)

Dav*_*vid 5 go docker docker-compose hot-reload

我已经尝试了所有方法,但没有任何方法可以解决我的热重加载问题,容器将正常加载并且代码将被构建,但是,修改代码后,代码将发生变化,但空气包不会进行任何重建。

如果编辑某些代码,此状态不会改变。 在此输入图像描述

如果在本地运行一切正常。 在此输入图像描述

Dockerfile:

FROM golang:alpine
ENV GO111MODULE=on

EXPOSE 8080

RUN mkdir /app
WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download
RUN go get github.com/cosmtrek/air

COPY . .

ENTRYPOINT ["air", "-c", ".air.toml"]
Run Code Online (Sandbox Code Playgroud)

docker-compose.yml

   go:
    container_name: go
    build:
      dockerfile: Dockerfile
      context: ./
    volumes:
      - ./:/app
    ports:
      - '8080:8080'
Run Code Online (Sandbox Code Playgroud)

.air.toml

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
Run Code Online (Sandbox Code Playgroud)

小智 5

完美的解决方案

问题: Air 正在启动,但未根据项目中的更改重新加载

解决方案:在 .air.toml 文件中添加poll = true这是因为 Windows 有一些限制,为了克服我们将 poll 设置为 true

代码中的解决方案:

poll = true
Run Code Online (Sandbox Code Playgroud)


小智 0

只需通过命令重建容器即可docker-compose up -d --build