skip to Main Content

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

3

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search