Golang is an awesome language for web development, but one annoying thing that I hear lots of people complain about is recompiling. Especially when using Docker it can be particularly frustrating. Here and there I hear about different ways of auto-reloading but I haven’t found many posts walking through the steps to set up auto-reloading a Golang project in Docker.
After spending quite a bit of time trying out and comparing different solutions for auto-reloading I found that CompileDaemon was the best solution for a couple reasons.
- The project works as intended
- It is simple to use
- Doesn’t require any hooks in the code
How to make it work in Docker
Below I have a very basic example of how to setup an auto-reloading project. There are two important options which are required.
-build– the build process to build the project.
-command– the command to run the project.
In this project below uses a normal build process, so just
go build -o app . and because the project is Golang the command is just the executable.
FROM golang:1.11-alpine RUN apk add --no-cache ca-certificates git RUN go get github.com/githubnemo/CompileDaemon WORKDIR /www COPY . . CMD CompileDaemon -build="go build -o app ." -command="/www/app"
Most Dockerfiles which are used in the real world are more complicated then this but the general idea works. Although this tutorial is for Golang, this can be used in any language!