We’ve been using go-watcher for local development with docker-compose to automatically rebuild a given service when its source code changes. From Go 1.20+ onwards the go-watcher binary no longer works, because it’s using a build flag -i which has been deprecated for a long time, but now is finally removed. Because of that the compilation fails with error. See it here.

The project is no longer maintained, and it seems that the authors are not going to update it.

Now I’m using a replacement called guard. Usage is almost identical as you can see in the examples below.

Note: we commit vendor, that’s why we build/install with -mod=vendor

Example Dockerfile using go-watcher to watch and rebuild a service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
FROM golang:1.19.5

RUN go install github.com/canthefason/go-watcher/cmd/[email protected]

ADD . /go/src/gitlab.com/myuser/myproject

WORKDIR /go/src/gitlab.com/myuser/myproject

RUN go install -mod=vendor ./cmd/svcmain/...

EXPOSE 8080

ENTRYPOINT ["sh", "-c", "/go/bin/watcher -run gitlab.com/myuser/myproject/cmd/svcmain -watch gitlab.com/myuser/myproject"]

Example Dockerfile using guard to watch and rebuild a service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM golang:1.20.1

RUN go install github.com/ysmood/kit/cmd/[email protected]

ADD . /go/src/gitlab.com/myuser/myproject

WORKDIR /go/src/gitlab.com/myuser/myproject

EXPOSE 8080

ENTRYPOINT ["sh", "-c", "/go/bin/guard -w '**/*.go' -- go run -mod=vendor ./cmd/svcmain/..."]