[英]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 上确实可以正常工作,因此我们目前可以选择的唯一解决方案如下:
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:实现这一目标的步骤是:
Install the Ubuntu distribution from the command line从命令行安装 Ubuntu 发行版wsl --install -d Ubuntu
Set ubuntu as the current distribution for WSL2将 ubuntu 设置为 WSL2 的当前分布
wsl --set-version Ubuntu 2
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 应用更改
Access the ubuntu filesystem from windows explorer \\wsl$\Ubuntu\
and move the repository copy to it.从 windows explorer \\wsl$\Ubuntu\
访问 ubuntu 文件系统并将存储库副本移动到它。
Install the extension for vscode Remote - WSL安装 vscode Remote 的扩展 - WSL
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 中打开文件夹
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.