Golang setup docker with hot reloading
Today I’m going to show you how to easily add hot reloading functionality to your Golang project. I will use a simple Gin project as an example and docker-compose.
To make code as short as possible I will use Gin example
with GET ping
endpoint returning {"message": "pong"}
as a response to request.
main.py
|
|
docker/Dockerfile
|
|
At the end of the file, we are running CompileDaemon
written in pure golang who will do all the job for us. In build
we can optionally put out own command to build project
by default is go build
. command
argument will run our binary created by build. Yes, is so easy :)
docker-compose.yml
|
|
Testing time!
Run docker-compose up --build
and check if I not lied to you :)
...
app_1 | Running build command!
app_1 | Build ok.
app_1 | Restarting the given command.
app_1 | stdout: [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
app_1 | stdout:
app_1 | stdout: [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
app_1 | stdout: - using env: export GIN_MODE=release
app_1 | stdout: - using code: gin.SetMode(gin.ReleaseMode)
app_1 | stdout:
app_1 | stdout: [GIN-debug] GET /ping --> main.main.func1 (3 handlers)
app_1 | stdout: [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
app_1 | stdout: [GIN-debug] Listening and serving HTTP on :8080
Change ping
endpoint response to something else and save your file.
You should see in your terminal new lines regarding rebuilding.
...
app_1 | Running build command!
app_1 | Build ok.
app_1 | Hard stopping the current process..
app_1 | Restarting the given command.
...
Full source from this post you can find here, have fun!