DEV Community

Thao Nguyen
Thao Nguyen

Posted on

[Go Tour ] 3. Hot-reload

1. What is Hot-reload?

Hot-reload or also known as Auto-reload, It’s like when you make a change to the source code and it automatically takes effect without a manual rebuild or restart. It helps to eliminate those repetitive and tedious tasks.. It saves and increase productivity.
The traditional technique to achieve hot-reload in Golang is to install a third-party tools or libraries libraries such as Air, Fresh, …etc. However, in this section, we will explore how to configure hot-reload without any third-party tools neither nor libraries. That will use the built-in compose watch a new feature of Docker Compose v2.22.

2. Docker Compose Watch (v2.22)

Docker Compose Watch is a new feature of Docker Compose v2.22. It allows us to watch the changes of the source code and automatically rebuild the image. It’s like a hot-reload feature for Docker Compose. It’s very useful for the local development environment. It helps to eliminate the repetitive and tedious tasks of rebuilding the image and restarting the container. It saves and increase productivity. https://docs.docker.com/compose/file-watch/

Compose Watch versus bind mounts:

Compose supports sharing a host directory inside service containers. Watch mode does not replace this functionality but exists as a companion specifically suited to developing in containers.

More importantly, watch allows for greater granularity than is practical with a bind mount. Watch rules let you ignore specific files or entire directories within the watched tree.

For example, in a JavaScript project, ignoring the node_modules/ directory has two benefits:

Performance. File trees with many small files can cause high I/O load in some configurations
Multi-platform. Compiled artifacts cannot be shared if the host OS or architecture is different to the container
For example, in a Node.js project, it's not recommended to sync the node_modules/ directory. Even though JavaScript is interpreted, npm packages can contain native code that is not portable across platforms.
Enter fullscreen mode Exit fullscreen mode

3. Auto-reload using Docker Compose Watch


--- a/gotour/3.setup-hot-reload/compose.yaml
+++ b/gotour/3.setup-hot-reload/compose.yaml
@@ -1,7 +1,6 @@
 version: '3.7'

 services:
-
   basic-svc:
     build:
       context: ./basic-svc
@@ -9,6 +8,10 @@ services:
       target: ${DOCKER_BUILD_TARGET:-dev}
     ports:
     - 8080:8080
+    develop:
+      watch:
+        - action: rebuild
+          path: ./basic-svc
   basic-ui:
     build: 
       context: ./basic-ui
@@ -16,3 +19,12 @@ services:
       target: ${DOCKER_BUILD_TARGET:-dev}
     ports:
       - 3000:3000
+    develop:
+      watch:
+        - action: sync
+          path: ./basic-ui
+          target: /src
+          exclude:
+            - node_modules
+        - action: rebuild
+          path: ./basic-ui/Dockerfile
\ No newline at end of file

Enter fullscreen mode Exit fullscreen mode

For the basic-svc service, we use the action: rebuild to rebuild the image when the source code changes. As outlined in the prior article, If the Dockerfile is optimized, the rebuild process should be fast.



For the basic-ui service, we use the action: sync to sync the source code to the container when the source code changes (the auto-reload frontend application will be handled by the frontend framework).

4. Conclusion

There are several ways to do hot-reload in Golang. In this article, we delved into to configuring hot-reload without relying on any third-party tools neither nor libraries. This approach will keep the Dockerfile simple and clean, facilitating a streamlined development environment.

Top comments (0)