I'm confused about the proper way to do hot deployment when developing a node js app while using S2I for building images. With Docker, the answer is to use bind mounts, which allows changes made on the host be become visible to nodemon running inside the container. The S2I docs mention that setting an environment variable DEV_MODE=true while starting the container from the image that it output enables hot deployment, but the nodemone running inside the container watches for changes inside the container not for changes on the host. Doing bind mounts doesn't work because the node_modules directory is not present on the host but only in the container, and that causes module not found errors. It seems like I would have to bind mount when starting the container, but then issue a mount of node_modules directory inside the container, but that doesn't sound right. What's the proper node js S2I development workflow setup?
1 Answers
I discovered a way to accomplish this using rsync.
I defined a "hot" development script in package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --ignore node_modules/ index.js",
"hot": "./hot.sh",
"start": "node index.js"
},
The hot script is a bash script that starts two instances of nodemon. The first instance and looks for changes in the /host directory, which will be bind-mounted into the container, and starts rsync to copy changes to the container's source directory. The second instance looks for changes in the container's source directory and restarts the server. In this example, only the index.js file is rsync'ed. Also, I had to start the first instance in the background.
echo "*** HOT ***"
nodemon --watch /host --exec "rsync -avP /host/index.js ./index.js" &
nodemon --ignore node_modules/ index.js
Building with a plain s2i build.
s2i build . registry.cirrus.ibm.com/ubi8/nodejs-16 cb-nodejs-16
Running with the NPM_RUN environment variable set to hot and bind mounting the current directory with the source code to the container's /host directory.
docker run -dp 8080:8080 --rm --env NPM_RUN=hot --volume "$(pwd)":/host cb-nodejs-16
With this setup I can make quickly make small changes on the host and have the server restart automatically in the container.

- 87
- 1
- 7