skip to Main Content

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

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!

2

Answers


  1. Try with

    bin = "./tmp/main.exe

    instead of

    bin = "tmp/main.exe"

    Hope that this will help you.

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

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