I am trying to auto-reload golang apps inside docker. I am using cosmtrek/air for doing it and it seems to be fine in my windows 10 machine. But when I am using docker for doing it, the code is not rebuilding. Here is the file structure
-cmd
-api
-main.go
.air.toml
docker-compose.dev.yml
Dockerfile.dev
go.mod
go.sum
This is my dockerfile
FROM golang:1.18.3-alpine3.15
WORKDIR /app
COPY go.mod go.sum /app/
RUN go mod download && go mod verify
RUN go install github.com/cosmtrek/air@latest
COPY ./ /app/
CMD "air"
Here is my docker-compose.dev.yml file
version: '3.8'
services:
backend:
container_name: go-backend-test
build:
context: .
dockerfile: ./Dockerfile.dev
volumes:
- ./:/app
This is the output I get in the logs

The issue is if I change anything in the main.go or any go files, the logs are not getting updating with the new code even through I have sh into the docker-container where the volumes are getting updating. Its seems to be not rebuilding. However it works fine and rebuilds in my windows machine. This is my .air.toml file
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "tmp/main.exe"
cmd = "go build -o ./tmp/main.exe ./cmd/api/."
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 = true
[screen]
clear_on_rebuild = true
Just burning my head on the topic all day. Thanks in advance for ideas!
I hit the same problem on windows with docker desktop.
To resolve it, I activated the poll method so I don't have to rely on fsnotify, as it's not fully working on windows.
Also, I needed to add the poll frequency. Otherwise the hot reload will only work during the ~10 first second after the container is started.
Here's a simple example of .air.toml
[build]
cmd = "go build -o ./tmp/main ./"
# required on windows, fsnotify is not reliable on windows
poll = true
poll_interval = 500
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With