实时自动重新加载golang应用程序 – Cosmtrek/air

huangapple go评论76阅读模式
英文:

Live Auto Reload of golang apps - Cosmtrek/air

问题

我正在尝试在Docker中自动重新加载Golang应用程序。我正在使用cosmtrek/air来实现,并且在我的Windows 10机器上似乎运行良好。但是当我在Docker中使用它时,代码没有重新构建。

这是我的文件结构:

-cmd
 -api
  -main.go
.air.toml
docker-compose.dev.yml
Dockerfile.dev
go.mod
go.sum

这是我的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"

这是我的docker-compose.dev.yml文件:

version: '3.8'
services:
  backend:
    container_name: go-backend-test
    build: 
      context: .
      dockerfile: ./Dockerfile.dev
    volumes:
      - ./:/app

这是我在日志中得到的输出:

实时自动重新加载golang应用程序 – Cosmtrek/air

问题是,如果我更改main.go或任何go文件中的任何内容,日志不会更新为新代码,即使我已经进入了容器,其中卷正在更新。它似乎没有重新构建。但是在我的Windows机器上,它可以正常工作并重新构建。

这是我的.air.toml文件:

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

整天都在研究这个问题,提前感谢您的建议!

英文:

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
实时自动重新加载golang应用程序 – Cosmtrek/air

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!

答案1

得分: 1

你的代码没问题。

问题出在Docker在主机系统和容器之间使用的文件共享技术上。

我在Mac上遇到了相同的问题,当我尝试使用不是Docker Desktop而是像Rancher这样的替代品时,甚至使用了通过moby的Docker CLI。当我切换回使用gRPC FUSE、osxfs和VirtioFS的原始Docker Desktop时,所有这些问题都迎刃而解。

英文:

Your code is okay.

The problem is with the technology docker uses for file sharing between a host system and containers.

I have had the same issues on mac when tried to use not the Docker Desktop but an alternative like Rancher which even uses docker CLI via moby. When I switched back to the original Docker Desktop which uses gRPC FUSE, osxfs, and VirtioFS — with all of them worked like a charm.

答案2

得分: -1

请尝试使用以下代码:

> bin = "./tmp/main.exe"

而不是

> bin = "tmp/main.exe"

希望这对你有所帮助。

英文:

Try with

> bin = "./tmp/main.exe

instead of

> bin = "tmp/main.exe"

Hope that this will help you.

huangapple
  • 本文由 发表于 2022年6月8日 21:28:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/72546523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定