Skip to content

Commit

Permalink
Rebasing and adding inti-wrapper for node 22
Browse files Browse the repository at this point in the history
  • Loading branch information
Costas Papastathis authored and github-actions[bot] committed Sep 25, 2024
1 parent 1967b4d commit aa852d7
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.c10s
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ RUN INSTALL_PKGS="nodejs$NODEJS_VERSION nodejs-nodemon nodejs$NODEJS_VERSION-ful
rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.c8s
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ RUN INSTALL_PKGS="nodejs nodejs-nodemon nodejs-full-i18n npm findutils tar which
rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.c9s
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ RUN INSTALL_PKGS="nodejs nodejs-nodemon nodejs-full-i18n npm findutils tar which
rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.fedora
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ RUN INSTALL_PKGS="nodejs$NODEJS_VERSION nodejs-nodemon nodejs$NODEJS_VERSION-ful

#
COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.rhel8
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ RUN INSTALL_PKGS="nodejs nodejs-nodemon nodejs-full-i18n npm findutils tar which
rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22-minimal/Dockerfile.rhel9
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ RUN INSTALL_PKGS="nodejs nodejs-nodemon nodejs-full-i18n npm findutils tar which
rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

COPY ./s2i/bin/ /usr/libexec/s2i
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
46 changes: 46 additions & 0 deletions 22-minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ Application developers can use the following environment variables to configure
**`NPM_RUN`**
Select an alternate / custom runtime mode, defined in your `package.json` file's [`scripts`](https://docs.npmjs.com/misc/scripts) section (default: npm run "start"). These user-defined run-scripts are unavailable while `DEV_MODE` is in use.

**`NODE_CMD`**
When specified (e.g.Specify `NODE_CMD="node server.js"`) the value of `NODE_CMD` is used to start the application instead of `npm start`.

**`INIT_WRAPPER`**
When set to "true", the application is started via the `init-wrapper` script instead of using `npm start`, by looking for the presence of the files `server.js`, `index.js` or `main.js` in the order in which they are listed. In case of `NODE_CMD` environemnt variale is specified, then `init-wrapper` script will use the value of `NODE_CMD` to start your application.

#### Additional variables used in the full-sized image

**`HTTP_PROXY`**
Expand Down Expand Up @@ -316,7 +322,47 @@ Below is an example _package.json_ file with the _main_ attribute and _start_ sc
#### Note:
`oc rsync` is only available in versions 3.1+ of OpenShift.

## init-wrapper

init-wrapper script is located on `/usr/libexec/s2i/init-wrapper` and is used to handle:

- Proper signal handling and propagation, as Node.js was not designed to run as PID 1.
- Reaping zombie child processes
Avoiding use of npm, there is more information on why you want to avoid that in the [Node.js reference architecture](https://github.com/nodeshift/nodejs-reference-architecture/blob/e4c4dc1fd20c2cac392e862859aaad27f85d504f/docs/development/building-good-containers.md#avoiding-using-npm-to-start-application). When the INIT_WRAPPER is set to true the application is started via the init script instead of using npm start.

A detailed explanation on how the init-wrapper script works is avalable in
[this url](http://veithen.io/2014/11/16/sigterm-propagation.html).

Example of using init-wrapper:

**During image build**
```
s2i -e INIT_WRAPPER=true build . buildImage node-app
docker run node-app
```
**During container start**
```
s2i build . buildImage node-app
docker run -e INIT_WRAPPER=true node-app
```

`init-wrapper` script can be disabled by setting the `INIT_WRAPPER` env variable to `false`.

```
docker run -e INIT_WRAPPER=false node-app
```
`NODE_CMD` can be used during the build process or container start, in order to have more control on the command that `init-wrapper` script will wrap.

For example:

**during container build**
```
s2i -e INIT_WRAPPER=true -e NODE_CMD="node index.js" build . buildImage node-app
```
**during container start**
```
docker run -e INIT_WRAPPER=false -e NODE_CMD="node index.js" node-app
```
See also
--------
Dockerfile and other sources are available on https://github.com/sclorg/s2i-nodejs-container.
Expand Down
18 changes: 18 additions & 0 deletions 22-minimal/s2i/bin/init-wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Overview of how this script works: http://veithen.io/2014/11/16/sigterm-propagation.html
# Set a trap to kill the main app process when this
# init script receives SIGTERM or SIGINT
trap 'kill -s TERM $PID' TERM INT
# Execute the main application in the background
"$@" &
PID=$!
# wait command always terminates when trap is caught, even if the process hasn't finished yet
wait $PID
# Remove the trap and wait till the app process finishes completely
trap - TERM INT
# We wait again, since the first wait terminates when trap is caught
wait $PID
# Exit with the exit code of the app process
STATUS=$?
exit $STATUS
23 changes: 23 additions & 0 deletions 22-minimal/s2i/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ run_node() {
if [ "$DEV_MODE" == true ]; then
echo "Launching via nodemon..."
exec nodemon --inspect="$DEBUG_PORT"
elif [ -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == true ]; then
echo "launching via init wrapper..."
exec ${STI_SCRIPTS_PATH}/init-wrapper $NODE_CMD
elif [ -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == false ]; then
echo "Launching via ${NODE_CMD}"
exec $NODE_CMD
elif [ ! -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == true ]; then

package_json_start=$(sed -n 's/\s*"start"\s*:\s*"\(.*\)".*/\1/p' package.json)
package_json_main=$(sed -n 's/\s*"main"\s*:\s*"\(.*\)".*/\1/p' package.json)

if [ -n "$package_json_start" ]; then
start_command=$package_json_start
elif [ -n $package_json_main ]; then
start_command="node ."
elif [ -f "server.js" ]; then
start_command="node server.js"
else
echo "Failed to find file for starting the Node.js application"
exit 1
fi
echo "launching via init wrapper..."
exec ${STI_SCRIPTS_PATH}/init-wrapper $start_command
else
echo "Launching via npm..."
exec npm run -d $NPM_RUN
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.c10s
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RUN INSTALL_PKGS="make gcc gcc-c++ git openssl-devel nodejs nodejs-nodemon nodej

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.c8s
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RUN yum -y module enable nodejs:$NODEJS_VERSION && \

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.c9s
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RUN dnf -y module enable nodejs:$NODEJS_VERSION && \

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.fedora
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ RUN INSTALL_PKGS="make gcc gcc-c++ libatomic_ops git openssl-devel nodejs$NODEJS

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image, including help file.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.rhel8
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RUN dnf -y module enable nodejs:$NODEJS_VERSION && \

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
1 change: 1 addition & 0 deletions 22/Dockerfile.rhel9
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ RUN dnf -y module enable nodejs:$NODEJS_VERSION && \

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH
RUN chmod +x /usr/libexec/s2i/init-wrapper

# Copy extra files to the image.
COPY ./root/ /
Expand Down
47 changes: 47 additions & 0 deletions 22/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ Application developers can use the following environment variables to configure
**`NPM_BUILD`**
Select an alternate / custom build command, defined in your `package.json` file's [`scripts`](https://docs.npmjs.com/misc/scripts) section (default: npm run "build"). These user-defined run-scripts are unavailable while `DEV_MODE` is in use.

**`NODE_CMD`**
When specified (e.g.Specify `NODE_CMD="node server.js"`) the value of `NODE_CMD` is used to start the application instead of `npm start`.

**`INIT_WRAPPER`**
When set to "true", the application is started via the `init-wrapper` script instead of using `npm start`, by looking for the presence of the files `server.js`, `index.js` or `main.js` in the order in which they are listed. In case of `NODE_CMD` environemnt variale is specified, then `init-wrapper` script will use the value of `NODE_CMD` to start your application.

**`NPM_RUN`**
Select an alternate / custom runtime mode, defined in your `package.json` file's [`scripts`](https://docs.npmjs.com/misc/scripts) section (default: npm run "start"). These user-defined run-scripts are unavailable while `DEV_MODE` is in use.

Expand Down Expand Up @@ -244,6 +250,47 @@ Below is an example _package.json_ file with the _main_ attribute and _start_ sc
#### Note:
`oc rsync` is only available in versions 3.1+ of OpenShift.

## init-wrapper

init-wrapper script is located on `/usr/libexec/s2i/init-wrapper` and is used to handle:

- Proper signal handling and propagation, as Node.js was not designed to run as PID 1.
- Reaping zombie child processes
Avoiding use of npm, there is more information on why you want to avoid that in the [Node.js reference architecture](https://github.com/nodeshift/nodejs-reference-architecture/blob/e4c4dc1fd20c2cac392e862859aaad27f85d504f/docs/development/building-good-containers.md#avoiding-using-npm-to-start-application). When the INIT_WRAPPER is set to true the application is started via the init script instead of using npm start.

A detailed explanation on how the init-wrapper script works is avalable in
[this url](http://veithen.io/2014/11/16/sigterm-propagation.html).

Example of using init-wrapper:

**During image build**
```
s2i -e INIT_WRAPPER=true build . buildImage node-app
docker run node-app
```
**During container start**
```
s2i build . buildImage node-app
docker run -e INIT_WRAPPER=true node-app
```

`init-wrapper` script can be disabled by setting the `INIT_WRAPPER` env variable to `false`.

```
docker run -e INIT_WRAPPER=false node-app
```
`NODE_CMD` can be used during the build process or container start, in order to have more control on the command that `init-wrapper` script will wrap.

For example:

**during container build**
```
s2i -e INIT_WRAPPER=true -e NODE_CMD="node index.js" build . buildImage node-app
```
**during container start**
```
docker run -e INIT_WRAPPER=false -e NODE_CMD="node index.js" node-app
```

See also
--------
Expand Down
18 changes: 18 additions & 0 deletions 22/s2i/bin/init-wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Overview of how this script works: http://veithen.io/2014/11/16/sigterm-propagation.html
# Set a trap to kill the main app process when this
# init script receives SIGTERM or SIGINT
trap 'kill -s TERM $PID' TERM INT
# Execute the main application in the background
"$@" &
PID=$!
# wait command always terminates when trap is caught, even if the process hasn't finished yet
wait $PID
# Remove the trap and wait till the app process finishes completely
trap - TERM INT
# We wait again, since the first wait terminates when trap is caught
wait $PID
# Exit with the exit code of the app process
STATUS=$?
exit $STATUS
23 changes: 23 additions & 0 deletions 22/s2i/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ run_node() {
if [ "$DEV_MODE" == true ]; then
echo "Launching via nodemon..."
exec nodemon --inspect="$DEBUG_PORT"
elif [ -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == true ]; then
echo "launching via init wrapper..."
exec ${STI_SCRIPTS_PATH}/init-wrapper $NODE_CMD
elif [ -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == false ]; then
echo "Launching via ${NODE_CMD}"
exec $NODE_CMD
elif [ ! -n "$NODE_CMD" ] && [ "$INIT_WRAPPER" == true ]; then

package_json_start=$(sed -n 's/\s*"start"\s*:\s*"\(.*\)".*/\1/p' package.json)
package_json_main=$(sed -n 's/\s*"main"\s*:\s*"\(.*\)".*/\1/p' package.json)

if [ -n "$package_json_start" ]; then
start_command=$package_json_start
elif [ -n $package_json_main ]; then
start_command="node ."
elif [ -f "server.js" ]; then
start_command="node server.js"
else
echo "Failed to find file for starting the Node.js application"
exit 1
fi
echo "launching via init wrapper..."
exec ${STI_SCRIPTS_PATH}/init-wrapper $start_command
else
echo "Launching via npm..."
exec npm run -d $NPM_RUN
Expand Down

0 comments on commit aa852d7

Please # to comment.