7647d70
NodeJS 10 container image
7647d70
===================
7647d70
7647d70
This container image includes Node.JS 10 as a [S2I](https://github.com/openshift/source-to-image) base image for your Node.JS 10 applications.
7647d70
Users can choose between RHEL, CentOS and Fedora based images.
7647d70
The RHEL images are available in the [Red Hat Container Catalog](https://access.redhat.com/containers/),
7647d70
the CentOS images are available on [Podman Hub](https://hub.docker.com/r/centos/),
7647d70
and the Fedora images are available in [Fedora Registry](https://registry.fedoraproject.org/).
7647d70
The resulting image can be run using [podman](https://github.com/containers/libpod).
7647d70
7647d70
Note: while the examples in this README are calling `podman`, you can replace any such calls by `docker` with the same arguments
7647d70
7647d70
Description
7647d70
-----------
7647d70
7647d70
Node.js 10 available as container is a base platform for 
7647d70
building and running various Node.js 10 applications and frameworks. 
7647d70
Node.js is a platform built on Chrome's JavaScript runtime for easily building 
7647d70
fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model 
7647d70
that makes it lightweight and efficient, perfect for data-intensive real-time applications 
7647d70
that run across distributed devices.
7647d70
7647d70
Usage
7647d70
---------------------
7647d70
For this, we will assume that you are using the `ubi8/nodejs-10 image`, available via `nodejs:10` imagestream tag in Openshift.
7647d70
Building a simple [nodejs-sample-app](https://github.com/sclorg/s2i-nodejs-container/tree/master/10/test/test-app) application
7647d70
in Openshift can be achieved with the following step:
7647d70
7647d70
    ```
7647d70
    oc new-app nodejs:10~https://github.com/sclorg/s2i-nodejs-container.git --context-dir=10/test/test-app/
7647d70
    ```
7647d70
7647d70
The same application can also be built using the standalone [S2I](https://github.com/openshift/source-to-image) application on systems that have it available:
7647d70
7647d70
    ```
7647d70
    $ s2i build https://github.com/sclorg/s2i-nodejs-container.git --context-dir=10/test/test-app/ ubi8/nodejs-10 nodejs-sample-app
7647d70
    ```
7647d70
7647d70
**Accessing the application:**
7647d70
```
7647d70
$ curl 127.0.0.1:8080
7647d70
```
7647d70
7647d70
Environment variables
7647d70
---------------------
7647d70
7647d70
Application developers can use the following environment variables to configure the runtime behavior of this image:
7647d70
7647d70
**`NODE_ENV`**  
7647d70
       NodeJS runtime mode (default: "production")
7647d70
7647d70
**`DEV_MODE`**  
7647d70
       When set to "true", `nodemon` will be used to automatically reload the server while you work (default: "false"). Setting `DEV_MODE` to "true" will change the `NODE_ENV` default to "development" (if not explicitly set).
7647d70
7647d70
**`NPM_RUN`**  
7647d70
       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.
7647d70
7647d70
**`HTTP_PROXY`**  
7647d70
       Use an npm proxy during assembly
7647d70
7647d70
**`HTTPS_PROXY`**  
7647d70
       Use an npm proxy during assembly
7647d70
7647d70
**`NPM_MIRROR`**  
7647d70
       Use a custom NPM registry mirror to download packages during the build process
7647d70
7647d70
One way to define a set of environment variables is to include them as key value pairs in your repo's `.s2i/environment` file.
7647d70
7647d70
Example: DATABASE_USER=sampleUser
7647d70
7647d70
#### NOTE: Define your own "`DEV_MODE`":
7647d70
7647d70
The following `package.json` example includes a `scripts.dev` entry.  You can define your own custom [`NPM_RUN`](https://docs.npmjs.com/cli/run-script) scripts in your application's `package.json` file.
7647d70
7647d70
#### Note: Setting logging output verbosity
7647d70
To alter the level of logs output during an `npm install` the npm_config_loglevel environment variable can be set. See [npm-config](https://docs.npmjs.com/misc/config).
7647d70
7647d70
Development Mode
7647d70
---------------------
7647d70
This image supports development mode. This mode can be switched on and off with the environment variable `DEV_MODE`. `DEV_MODE` can either be set to `true` or `false`.
7647d70
Development mode supports two features:
7647d70
* Hot Deploy
7647d70
* Debugging
7647d70
7647d70
The debug port can be specified with the environment variable `DEBUG_PORT`. `DEBUG_PORT` is only valid if `DEV_MODE=true`.
7647d70
7647d70
A simple example command for running the container in development mode is:
7647d70
```
7647d70
podman run --env DEV_MODE=true my-image-id
7647d70
```
7647d70
7647d70
To run the container in development mode with a debug port of 5454, run:
7647d70
```
7647d70
$ podman run --env DEV_MODE=true DEBUG_PORT=5454 my-image-id
7647d70
```
7647d70
7647d70
To run the container in production mode, run:
7647d70
```
7647d70
$ podman run --env DEV_MODE=false my-image-id
7647d70
```
7647d70
7647d70
By default, `DEV_MODE` is set to `false`, and `DEBUG_PORT` is set to `5858`, however the `DEBUG_PORT` is only relevant if `DEV_MODE=true`.
7647d70
7647d70
Hot deploy
7647d70
--------------------
7647d70
7647d70
As part of development mode, this image supports hot deploy. If development mode is enabled, any souce code that is changed in the running container will be immediately reflected in the running nodejs application.
7647d70
7647d70
### Using Podman's exec
7647d70
7647d70
To change your source code in a running container, use Podman's [exec](https://github.com/containers/libpod) command:
7647d70
```
7647d70
$ podman exec -it <CONTAINER_ID> /bin/bash
7647d70
```
7647d70
7647d70
After you [Podman exec](https://github.com/containers/libpod) into the running container, your current directory is set to `/opt/app-root/src`, where the source code for your application is located.
7647d70
7647d70
### Using OpenShift's rsync
7647d70
7647d70
If you have deployed the container to OpenShift, you can use [oc rsync](https://docs.openshift.org/latest/dev_guide/copy_files_to_container.html) to copy local files to a remote container running in an OpenShift pod.
7647d70
7647d70
#### Warning:
7647d70
7647d70
The default behaviour of the s2i-nodejs container image is to run the Node.js application using the command `npm start`. This runs the _start_ script in the _package.json_ file. In developer mode, the application is run using the command `nodemon`. The default behaviour of nodemon is to look for the _main_ attribute in the _package.json_ file, and execute that script. If the _main_ attribute doesn't appear in the _package.json_ file, it executes the _start_ script. So, in order to achieve some sort of uniform functionality between production and development modes, the user should remove the _main_ attribute.
7647d70
7647d70
Below is an example _package.json_ file with the _main_ attribute and _start_ script marked appropriately:
7647d70
7647d70
```json
7647d70
{
7647d70
    "name": "node-echo",
7647d70
    "version": "0.0.1",
7647d70
    "description": "node-echo",
7647d70
    "main": "example.js", <--- main attribute
7647d70
    "dependencies": {
7647d70
    },
7647d70
    "devDependencies": {
7647d70
        "nodemon": "*"
7647d70
    },
7647d70
    "engine": {
7647d70
        "node": "*",
7647d70
        "npm": "*"
7647d70
    },
7647d70
    "scripts": {
7647d70
        "dev": "nodemon --ignore node_modules/ server.js",
7647d70
        "start": "node server.js" <-- start script
7647d70
    },
7647d70
    "keywords": [
7647d70
        "Echo"
7647d70
    ],
7647d70
    "license": "",
7647d70
}
7647d70
```
7647d70
7647d70
#### Note:
7647d70
`oc rsync` is only available in versions 3.1+ of OpenShift.
7647d70
7647d70
7647d70
See also
7647d70
--------
7647d70
Dockerfile and other sources are available on https://github.com/sclorg/s2i-nodejs-container.
7647d70
In that repository you also can find another versions of Python environment Dockerfiles.
7647d70
Dockerfile for CentOS is called `Dockerfile`, Dockerfile for RHEL7 is called `Dockerfile.rhel7`,
7647d70
for RHEL8 it's `Dockerfile.rhel8` and the Fedora Dockerfile is called Dockerfile.fedora.