简体   繁体   English

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

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

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.我已经尝试了所有方法都没有解决我的热重载问题,容器将正常加载并且代码将被构建,但是,在修改代码后,代码会改变,但是空气 package 不会做任何重建。

This state does not change if edit some code.如果编辑一些代码,这个 state 不会改变。 在此处输入图像描述

if run locally everything works fine.如果在本地运行一切正常。 在此处输入图像描述

Dockerfile: 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 docker-compose.yml

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

.air.toml .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

Simply rebuild container by command docker-compose up -d --build只需通过命令docker-compose up -d --build重建容器

The problem will be solved when you open your folders in visual studio using wsl当您使用 wsl 在 Visual Studio 中打开文件夹时,问题将得到解决

enter image description here在此处输入图像描述

may be helpful: https://code.visualstudio.com/docs/remote/wsl可能会有所帮助: https://code.visualstudio.com/docs/remote/wsl

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 .正如我在一些评论中所读到的,问题确实是由于 Air 使用事件通知 (fsnotify) 并且这不能在 windows 系统和 docker 容器之间正确传播 However, this does work correctly on linux, for this reason the only solution we can currently choose is the following:但是,这在 linux 上确实可以正常工作,因此我们目前可以选择的唯一解决方案如下:

Install WSL2 on Windows在 Windows 上安装 WSL2

WSL2 allows us to install a Linux distribution within our Windows system in order to use its tools, utilities and file system. WSL2 允许我们在我们的 Windows 系统中安装 Linux 发行版,以便使用它的工具、实用程序和文件系统。 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.多亏了这一点,我们将能够通过将存储库的副本移动到 linux 文件系统并对其进行处理来解决事件传播问题,但所有这些都在 windows 操作系统中。

The steps to follow to achieve this are:实现这一目标的步骤是:

  1. Install the Ubuntu distribution from the command line从命令行安装 Ubuntu 发行版
    wsl --install -d Ubuntu

  2. Set ubuntu as the current distribution for WSL2将 ubuntu 设置为 WSL2 的当前分布

    wsl --set-version Ubuntu 2

  3. Apply WSL integration in docker在 docker 中应用 WSL 集成

    3.1 Go to docker desktop -> settings -> resources -> WSL Integration -> Refresh 3.1 Go 到 docker 桌面 -> 设置 -> 资源 -> WSL 集成 -> 刷新

    3.2 Activate Ubuntu 3.2 激活 Ubuntu

    3.3 Apply changes 3.3 应用更改

  4. Access the ubuntu filesystem from windows explorer \\wsl$\Ubuntu\ and move the repository copy to it.从 windows explorer \\wsl$\Ubuntu\访问 ubuntu 文件系统并将存储库副本移动到它。

  5. Install the extension for vscode Remote - WSL安装 vscode Remote 的扩展 - WSL

  6. Open the working directory in vscode from the new location via remote wsl using:通过远程 wsl 使用以下命令从新位置打开 vscode 中的工作目录:

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

  7. Run the command:运行命令:

docker-compose up

All this information has been obtained from the open issue today in the air package repository.所有这些信息都是从今天在空中 package 存储库中的公开问题中获得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无服务器离线启动热重载不起作用 - serverless offline start hot reload not working package-lock.json 的链接类似于 git://github.com/danteata/passport-oauth.git - package-lock.json has links like git://github.com/danteata/passport-oauth.git Flutter:连接到 firebase 后台功能后,热重载和热重启不起作用 - Flutter: Hot reload and hot restart not working after connecting to firebase background functionality 如何在当前工作中保存图像并使用 docker 在 golang 中加载图像 - how to save the image in current working and load the image in golang using docker Git 克隆给出“ssh:连接到主机 github.com 端口 22:连接超时” Linux /opt 目录 Amazon EC2 实例 - Git clone gives "ssh: connect to host github.com port 22: Connection timed out" Linux /opt directory Amazon EC2 Instance 如何在 docker 中运行 golang 服务器 - How to run golang server in docker Golang 模板将 docker ps output 格式化为 JSON - Golang template to format docker ps output as JSON 上下文超时在golang中没有按预期工作 - Context timeout not working as expected in golang Golang - 创建 docker 图像的问题 - Golang - problem with creating docker image 使用 Golang 从 Github 图形 QL API 提供图像文件 - Serve Image File From Github Graph QL API Using Golang
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM