So, in this tutorial, I’ll show one easy way to create your Docker image once and have it reload on each code change just as you are used to in node.js let’s say, with the
We will have a simple Go web server implemented using a small and compact Express-like framework that you should definitely check out, called - Fiber. So, let’s write a simple hello world code. Here is the code.
Now when we have code in place we are ready to launch an application, but not just with a local Go binary but rather inside of a Docker container. So, here is the Dockerfile.
As you can see it’s fairly small and compact, but with this approach, we would need to do a rebuild of the image every time we change code, and in development, we don’t want that, of course. So, here is an updated version of Dockerfile that will just by adding these two lines to do all the magic for you.
We use the Air package present here on GitHub. What we do first is install
Air executable in our container with go get, then we set ENTRYPOINT to ["air"] which will basically run the script that will wrap go build command for you and start watching your files and folders for any change and react to those changes accordingly.
Air supports quite a few configuration options that you can check in the example file on the repo. Here for simplicity, I didn’t want to include any of that stuff, but usually what people do is change at least the entry file for generating executable. Here we have used just plain old
That’s it, neat and simple, happy live reloading.