在Docker中使用golang(github.com/cosmtrek/air)时,热重载(Hot Reload)不起作用。

huangapple go评论105阅读模式
英文:

Hot Reload not working in docker with golang (github.com/cosmtrek/air)

问题

我尝试了一切,但没有解决我的热重载问题。容器会正常加载并构建代码,但在修改代码后,代码会发生变化,但热重载工具不会重新构建。

如果在本地运行,一切都正常。

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"]

docker-compose.yml:

go:
  container_name: go
  build:
    dockerfile: Dockerfile
    context: ./
  volumes:
    - ./:/app
  ports:
    - '8080:8080'

.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

图片链接:
1: https://i.stack.imgur.com/YgADM.png
2: https://i.stack.imgur.com/84Rpx.png

英文:

I've tried everything and nothing has solved my hot reload problem, the containers will load normally and the code will be built, however, after modifying the code, the code will change, but the air package won't do any rebuilds.

This state does not change if edit some code.
在Docker中使用golang(github.com/cosmtrek/air)时,热重载(Hot Reload)不起作用。

if run locally everything works fine.
在Docker中使用golang(github.com/cosmtrek/air)时,热重载(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"]

docker-compose.yml

   go:
    container_name: go
    build:
      dockerfile: Dockerfile
      context: ./
    volumes:
      - ./:/app
    ports:
      - '8080:8080'

.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

答案1

得分: 2

我看到这是一个旧问题,但我最近遇到了这个问题,并最终成功解决了它。所以我在这里留下我的答案,以防对其他用户有所帮助。

根据我在一些评论中所读到的,问题确实是由于Air使用事件通知(fsnotify),而这个事件通知在Windows系统和Docker容器之间无法正确传播。然而,在Linux上,这个问题可以正确解决,因此我们目前唯一的解决方案是:

在Windows上安装WSL2

WSL2允许我们在Windows系统中安装Linux发行版,以便使用其工具、实用程序和文件系统。借助这个功能,我们将能够通过将代码库的副本移动到Linux文件系统并在其中工作来解决事件传播的问题,但所有这些都在Windows操作系统中进行。

实现这一目标的步骤如下:

  1. 通过命令行安装Ubuntu发行版
    wsl --install -d Ubuntu

  2. 将Ubuntu设置为当前的WSL2发行版
    wsl --set-version Ubuntu 2

  3. 在Docker中应用WSL集成

    3.1 打开Docker Desktop -> 设置 -> 资源 -> WSL集成 -> 刷新

    3.2 激活Ubuntu

    3.3 应用更改

  4. 从Windows资源管理器访问Ubuntu文件系统 \\wsl$\Ubuntu\,并将代码库的副本移动到其中。

  5. 安装VSCode的Remote - WSL扩展

  6. 通过远程WSL在新位置中的VSCode中打开工作目录:

    ctrl+shift+p -> 在WSL中打开文件夹

  7. 运行以下命令:
    docker-compose up

所有这些信息都是从今天在air软件包存储库中的开放问题中获取的。

英文:

I see this is an old question but I ran into this problem recently and finally managed to solve it. So I leave my answer here in case it helps other users.

As I have read in some comments, the problem is indeed due to the fact that Air uses event notification (fsnotify) and this does not propagate correctly between the windows system and the docker containers.
However, this does work correctly on linux, for this reason the only solution we can currently choose is the following:

Install WSL2 on Windows

WSL2 allows us to install a Linux distribution within our Windows system in order to use its tools, utilities and file system. Thanks to this, we will be able to solve the problem of event propagation by moving our copy of the repository to the linux file system and working on it, but all within the windows operating system.

The steps to follow to achieve this are:

  1. Install the Ubuntu distribution from the command line
    wsl --install -d Ubuntu

  2. Set ubuntu as the current distribution for WSL2

    wsl --set-version Ubuntu 2

  3. Apply WSL integration in docker

    3.1 Go to docker desktop -> settings -> resources -> WSL Integration -> Refresh

    3.2 Activate Ubuntu

    3.3 Apply changes

  4. Access the ubuntu filesystem from windows explorer \\wsl$\Ubuntu\ and move the repository copy to it.

  5. Install the extension for vscode Remote - WSL

  6. Open the working directory in vscode from the new location via remote wsl using:

    ctrl+shift+p -> Open folder in WSL

  7. Run the command:

docker-compose up

All this information has been obtained from the open issue today in the air package repository.

答案2

得分: 0

只需使用命令docker-compose up -d --build重新构建容器。

英文:

Simply rebuild container by command docker-compose up -d --build

答案3

得分: 0

当你使用WSL在Visual Studio中打开文件夹时,问题将会得到解决。

可能会有帮助的链接:
https://code.visualstudio.com/docs/remote/wsl

英文:

The problem will be solved when you open your folders in visual studio using wsl

enter image description here

may be helpful:
https://code.visualstudio.com/docs/remote/wsl

huangapple
  • 本文由 发表于 2021年12月12日 15:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/70321633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定