Note: Use docker to implement hot loading function when developing vue, react or node, that is, after the host file is modified, the service is refreshed in real time or restarted in real time.
Don't use docker
When developing node programs locally, only configuration is requirednodemon
Just start, for example
Suppose you have a simple file named, the content is as follows:
const http = require('http'); const server = ((req, res) => { (200, {'Content - Type': 'text/plain'}); ('Hello, World!\n'); }); (3000, () => { ('Server running on port 3000'); });
Then just run the following command
npm install -g nodemon nodemon
At this time, when you modifyAfter the file content,
nodemon
The file changes will be detected and the server will be automatically restarted. After refreshing the page, you will immediately see the modified effect.
Using docker
The docker version isDocker version 27.1.1, build 6312585
, and has been configureddockerfile
on the basis of the document. There are many ways to synchronize local files with files in containers. Here we will only introduce two methods of configuration, namely:
1. Use watch to synchronize the configuration (recommended):
services: server: build: context: . ports: - 3000:3000 develop: watch: - path: ./ # Listen to files in the current directory action: rebuild # Docker will be rebuilt when the file changes, which is equivalent to docker compose up --build - path: . # Listen to the current directory target: /usr/src/app # The target path in the container, associating the current directory of the host with the /usr/src/app path in the container ignore: # Ignored files - node_modules/ action: sync # Synchronize the action, docker will ensure that any changes made to the files on the host will automatically match the corresponding files in the service container. command: nodemon
Please read the comments carefully, you only need to run itdocker compose up --watch
The command can realize hot updates.For more configuration, please go to the official documentation to view
2. Use Bind Mounts to bind mounts (container file changes will be synchronized to the host):
services: server: build: context: . ports: - 3000:3000 volumes: - .:/usr/src/app # Bind the host's current directory to the container - /usr/src/app/node_modules # Create an anonymous volume to store the node_modules of the container to prevent the local node_modules directory from overwriting dependencies in the container. command: nodemon --legacy-watch --polling-interval 1000 # The command here is inconsistent with the above
Please read the comments carefully and run using this configurationdocker compose up
The command can also implement hot updates.
Notice:
I believe you have noticed it toocommand
Command and aboveInconsistent, this is because it runs directlynodemon
Hot loading failure will occur under Windows. The specific phenomenon is when modifying files in the container.nodemon
Can correctly detect changes and hot load the application; but when the host modifys the file, although the file has been synchronized into the container,nodemon
However, these changes were not detected, thus failing to trigger hot loading.
reason:In Docker environments, especially on macOS and Windows, file system events (e.g.inotify
Events) may not be properly delivered from the host to the container. This leads tonodemon
File changes on the host cannot be detected. So, although the file has been synchronized into the container,nodemon
The hot loading is still not triggered.
Solution:Configurationnodemon
Use polling, that isnodemon --legacy-watch --polling-interval 1000
The command implements the hot loading function. However, it should be noted that compared to the default mode, the polling mode consumes more CPU and I/O resources, especially when large-scale projects or frequent file changes. You can also try itdocker-sync
ormutagen
Wait for the tool to solve this problem, but I haven't tried it.
Summarize
1. Usewatch
Synchronous configuration
advantage:
- Advanced synchronization policy: Different synchronization policies can be set for specific files or directories, such as triggering rebuilds only when some files change.
- Automation process: Combining automatic reconstruction and synchronization, improve development efficiency and reduce manual operations.
- flexibility: Synchronous and reconstruction behaviors can be customized according to project needs to adapt to complex development processes.
shortcoming:
- Maintenance cost: Configuration may require more maintenance, especially when teamwork or project size is expanded.
Applicable scenarios:
- Most projects: Suitable for most projects, highly recommended.
2. Use Bind Mounts to bind mounts
advantage:
- Real-time synchronization: Any changes on the host will take effect immediately within the container and vice versa.
- Simple and intuitive: Simple configuration, directly map host directory to container directory.
shortcoming:
- Performance issues: On some operating systems such as macOS and Windows, the performance of binding mounts may be poor, especially when there are a large number of file operations. In addition, file system events may not be passed in time under Windows.
- Path dependency: The binding mount path depends on the host's file system structure and may affect cross-platform portability.
- Permission Management: It is necessary to ensure that the permissions of files in the host and container are consistent to avoid permission conflicts.
Applicable scenarios:
- Development Environment: The code needs to be modified frequently and you want these changes to be reflected in the running container immediately.
- Simple project: The project structure is simple and does not require complex file synchronization strategies.
The above is the detailed content of implementing hot loading functions when using Docker for node development. For more information about hot loading of Docker node, please pay attention to my other related articles!