• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Dockerfile
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sample Dockerfile templates for building Dev, Debug, Test and Run images for your application

Docker Templates for Node.js

This project uses the Universal Base Docker images built by Red Hat and provides guidance on how to add best-practise Dockerfiles into your project.

This project contains three template Dockerfiles:

Dockerfile Description
Dockerfile A default Dockerfile for building and running your Node.js application
Dockerfile-tools A Dockerfile for developing or debugging your Node.js application
Dockerfile-run A multi-stage Dockerfile for building a "run" image for your application, based on the UBI 8 Node.js Minimal image

In order to use these template files, copy the files from this project into your application directory.

Installing the Templates

The templates can most easily be installed into your project using either of the following mechanisms:

  • All Files:
    Use the "Clone or download" button and select "Download ZIP" to copy the entire set of templates, and copy those into your project.
  • Individual Files:
    Use "wget" to download individual files using:
    wget https://raw.githubusercontent.com/NodeShift/docker/main/{file}

Using Dockerfile

The Dockerfile template creates a Docker image for your application that:

  • Uses the registry.access.redhat.com/ubi8/nodejs-16:latest image

The template also makes the following assumptions about your application:

  • It listens on port 3000
  • It can be started using npm start

You can change these settings by updating the EXPOSE and CMD entries of the Dockerfile.

Building the Docker image for your application

After any required changes have been made to the Dockerfile, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application -f Dockerfile .

where my-nodejs-application is the name you want to give your created Docker image.

Running the Docker image for your application

After the Docker image has been created for your application, you can run it using either of the following commands:

  • Run as an interactive application on your command line:

    docker run -i -p 3000:3000 -t my-nodejs-application

    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping.

  • Run as a daemon process:

    docker run -d -p 3000:3000 -t my-nodejs-application

    This uses the -d flag rather than the -i flag to run the Docker image as a background task.

Using Dockerfile-tools

The Dockerfile-tools template creates a Docker image for your application that:

  • Uses the registry.access.redhat.com/ubi8/nodejs-16:latest image
  • Provides a script for running in dev mode using nodemon
  • Provides a script for running in debug mode using node --inspect

The template also makes the following assumptions about your application:

  • It listens on port 3000
  • It can be started using node server/server.js

You can change the port by editing the EXPOSE entry in the Dockerfile-tools file, and the start command by editing the run-dev and run-debug scripts.

Building the Docker tools image for your application

After any required changes have been made to the Dockerfile-tools, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application-tools -f Dockerfile-tools .

where my-nodejs-application-tools is the name you want to give your created tools Docker image.

Running the Docker tools image for your application: Development Mode

Running the image in Development Mode uses nodemon to watch for changes in your application and automatically restart it as those changes are made.

To enable your local changes to be updated in the Docker image, you must map your local file system into the running Docker container, as follows:

  1. Generate a Linux version of your node_modules dependencies locally, by generating them inside the node:16 docker image:
docker run -i -v "$PWD"/package.json:/tmp/package.json -v "$PWD"/node_modules_linux:/tmp/node_modules -w /tmp -t node:16 npm install

This step only needs to be repeated if you modify your package.json file.

  1. Run the Docker tools image as an interactive application on your command line in dev mode:
docker run -i -p 3000:3000 -v "$PWD"/:/app -v "$PWD"/node_modules_linux:/app/node_modules -t my-nodejs-application-tools /bin/run-dev

This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping. This command also maps your local directory into the Docker container, allowing you to modify your Node.js application code and see the changes running immediately in the container.

Running the Docker tools image for your application: Debug Mode

In order to run your application in debug mode:

  • Run as an interactive applications on your command line in debug mode:
    docker run -i -p 3000:3000 -p 9229:9229 -t my-nodejs-application-tools /bin/run-debug
    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping. This command also maps port 9229 in the image to the same port on your machine so that you can connect the debugger.

If you wish to run your Docker tools image as a background task, switch the -i flag to -d on the command line.

Using Dockerfile-run

The Dockerfile-run template creates a Docker image using a multi-stage build that:

  • Retrieves your dependencies and compiles any native add-ons using the registry.access.redhat.com/ubi8/nodejs-16:latest image
  • Copies your dependencies into the registry.access.redhat.com/ubi8/nodejs-16-minimal:latestimage for reduced size

The template also makes the following assumption about your application:

  • It listens on port 3000
  • It can be started using npm start

You can change these settings by updating the EXPOSE and CMD entries of the Dockerfile-run template.

Building the Docker run image for your application

After any required changes have been made to the Dockerfile-run file, you can build a Docker image for your application using the following command:

docker build -t my-nodejs-application-run -f Dockerfile-run .

where my-nodejs-application-run is the name you want to give your created Docker run image.

Running the Docker run image for your application

After the Docker run image has been created for your application, you can run it using either of the following commands:

  • Run as an interactive application on your command line:

    docker run -i -p 3000:3000 -t my-nodejs-application-run

    This maps port 3000 in the Docker image to port 3000 on your machine. If you are using a different port, you will need to change the mapping.

  • Run as a daemon process:

    docker run -d -p 3000:3000 -t my-nodejs-application-run

    This additionally uses the -d flag to run the Docker image as a background task.

Publishing the Image

In order to use your application's Docker image in a cloud it needs to be published. You can deploy to the image to the DockerHub registry using the following commands:

  1. Log in to DockerHub:

    docker login

    This logs you into DockerHub using your Docker ID, where you have a namespace that matches your DockerHub ID.

  2. Tag your application's Docker image with the name you want to use in DockerHub:

    docker tag my-nodejs-application-run <namespace>/nodeserver:1.0.0

    where <namespace> is your namespace in DockerHub.

  3. Publish your application's Docker image to DockerHub:

    docker push <namespace>/nodeserver:1.0.0

    where <namespace> is your namespace in DockerHub.

More Repositories

1

nodejs-reference-architecture

The Red Hat and IBM Node.js Reference architecture. The teams 'opinion' on what components our customers and internal teams should use when building Node.js applications and guidance for how to be successful in production with those components.
JavaScript
1,707
star
2

opossum

Node.js circuit breaker - fails fast ⚡️
JavaScript
1,282
star
3

nodeshift

CLI application for OpenShift Node.js deployment 🚀
JavaScript
83
star
4

helm

Sample Helm Chart templates for packaging your Node.js application for deployment to Kubernetes
81
star
5

tutorial

How to build and deploy your first Cloud Native Node.js application
55
star
6

openshift-rest-client

Node based Openshift REST admin client
JavaScript
25
star
7

opossum-prometheus

Prometheus metrics for opossum circuit breakers
JavaScript
21
star
8

npcheck

"Node Package Checker" - A tool to run various checks on npm modules
JavaScript
16
star
9

faas-js-runtime

A function invocation framework for Node.js and TypeScript
JavaScript
16
star
10

js-functions-standardization

Discussing standardizing serverless JS functions
14
star
11

kube-service-bindings

Kubernetes service bindings utility
JavaScript
14
star
12

mern-workshop

MERN example code to be deployed into Docker and Kubernetes
JavaScript
11
star
13

web-application-reference

WIP repo to hold Red Hat/IBM web application reference
11
star
14

v8-build

Scripts related to building V8 on IBM platforms.
Shell
2
star
15

ubi-nodejs-extension

Working repo for extension to Paketo buildpacks to support ubi
Go
2
star
16

nodeshift.github.io

Nodeshift website http://nodeshift.dev
HTML
1
star
17

nodejs_reference_architecture_module_stats_generator

JavaScript
1
star