Whilst I was working on the Go backend for a project recently that was running in a docker container, I became frustrated that most of the existing solutions for watching and restarting an app on change didn't work in a mounted volume. Those that did work required ridiculous amounts of configuration. This led me to create go watcher.
Install it with go get
and then run your source file with gow
instead of go
.
go get github.com/mrbbot/gow
gow main.go
If you're using Go 1.11's modules, these will be installed automatically.
If we have a web app in a file called main.go
,
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
_ = r.Run()
}
...and the Dockerfile
shown below running with a volume mounted to /app
,
FROM golang:1.11-alpine
RUN apk --update add git curl bash build-base
WORKDIR /app
RUN go get github.com/mrbbot/gow
CMD ["gow", "main.go"]
EXPOSE 8080
...anytime we change a file, the server will restart.