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.
if run locally everything works fine.
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
3
Answers
Simply rebuild container by command
docker-compose up -d --build
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
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:
Install the Ubuntu distribution from the command line
wsl --install -d Ubuntu
Set ubuntu as the current distribution for WSL2
wsl --set-version Ubuntu 2
Apply WSL integration in docker
3.1 Go to docker desktop -> settings -> resources -> WSL Integration -> Refresh
3.2 Activate Ubuntu
3.3 Apply changes
Access the ubuntu filesystem from windows explorer
\wsl$Ubuntu
and move the repository copy to it.Install the extension for vscode Remote – WSL
Open the working directory in vscode from the new location via remote wsl using:
ctrl+shift+p
-> Open folder in WSLRun the command:
docker-compose up
All this information has been obtained from the open issue today in the air package repository.