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

4

5

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. enter image description here

if run locally everything works fine. enter image description here

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
Perceive answered 12/12, 2021 at 7:6 Comment(6)
You havn't bind-mounted your source code into the container. I'm assuming your are modifying the code on your host, not inside the container?Riccio
@Riccio Exactly, however, after examining the file in the container, the code changes, but the library does not rebuild (volumes: - ./:/app ) -> this is not enough ?Perceive
Ah, right. That's a bind-mount. Missed that.Riccio
Maybe a better option to use could be skaffold (skaffold.dev)Caundra
Hi @David, did you happen to find a solution for it? I'm getting the same issue, i.e. local works but file changes not picking up when running in docker. I'm on Windows and suspecting that this has something to do with the filesystem hmm..Plunk
Hi @Plunk , unfortunately I am still in the same situation and I am developing on local, however I am using macos so this problem in my opinion must be done by docker.Perceive
C
6

The perfect solution 💕

Issue: Air is starting but not reloading on changes in the project

Solution: In .air.toml file add poll = true This is because Windows has some restrictions and to overcome that we set poll to true

Solution in code:

poll = true
Crescin answered 7/10, 2023 at 5:25 Comment(1)
Did it work, please let me knowCrescin
B
2

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.

Beeler answered 22/8, 2022 at 19:49 Comment(0)
L
0

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

Lilliamlillian answered 12/12, 2021 at 7:58 Comment(2)
if I change the code it will not work (not rebuilded). The state is only running, but it does not respond to a code change.Perceive
This does not solve the issue of live reload no matter how much you rebuild.Nickolai
C
0

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

Confute answered 24/6, 2022 at 19:17 Comment(1)
Link only answers are considered very low quality and can get deleted, please put the important parts from the linked resource into the answer body.Columniation

© 2022 - 2024 — McMap. All rights reserved.