---
# System prepended metadata

title: How to pack your own nodejs env into container ?

---

How to pack your own nodejs env into container ?
===

## Brief steps
1. Create the nodejs virtual environment by using nvm
2. Install the specific package that you want to test
3. Generate the package.json which record the node packages
4. Choose base image like [alpine](https://hub.docker.com/_/alpine) or [node](https://hub.docker.com/_/node)
5. Run container and mount the package.json into it
6. Install the node package during running
7. Test the function of node package and check dependiencies

## 1. Node virtual environment
### Install nvm
* Refer to [nvm github](https://github.com/nvm-sh/nvm#install--update-script)
    ```shell=
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash```
* Make sure that the nvm command works in your shell env
* For example, if your shell env is `zsh`, then you should update your `~/.zshrc` file as below:
    ```shell=
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
    ```

### List versions and install
* To install the stable version of node.js with nvm, use the commands as below:
    ```shell=
    nvm install node
    nvm list
    ```
* Check the node version if the command works.

## 2. Install package with node
* The command would install express and generate the package.json
    ```shell=
    npm install express
    ```
## 3. Generate and maintain the `package.json` file
* Because we just install the express by `npm install` without `npm init`, so there won't be other info in it.
![](https://i.imgur.com/0dWu4V6.png)
* You can just type `npm init` to complete the remain info.
![](https://i.imgur.com/VB7BMvw.png)
* You can test the express with the js code below, please write in `app.js`:
    ```javascript=
    var express = require('express');
    var app = express();

    app.get('/', function (req, res) {
      res.send('Hello World!');
    });

    app.listen(3000, function () {
      console.log('Example app listening on port 3000!');
    });
    ```
* Call `node app.js` and check with `curl -v localhost:3000`
    ![](https://i.imgur.com/ijKz4x0.png)
    ![](https://i.imgur.com/6dOB1c0.png)

## 4. Choose base image like `node`
* Search on the DockerHub ...
    ![](https://i.imgur.com/H2jCQJS.png)
* Select the first one cause it is the official image. It will be easy for you to start a node environment in several seconds.
    ![](https://i.imgur.com/DnIkK8K.png)
* Call the `docker pull node` command while the docker engine is running on your computer.
    ![](https://i.imgur.com/N6D63Z4.png)
    ![](https://i.imgur.com/bbHjb19.png)
* It takes you several seconds to a minute to download the image form internet if the image haven't been downloaded to your computer.
    ![](https://i.imgur.com/aHMK2oo.png)
    ![](https://i.imgur.com/ya0Jyrx.png)

## 5. Run the node container from image and mount the files.
* Try to run the container and bind mount the `package.json` and `app.js` files into the container.
* The command is as:
    ```shell=
    docker run -it --rm -v `pwd`:/code -p 5487:3000 node bash
    ```
    ![](https://i.imgur.com/xqGTcMx.png)
* We just mounted the host port 5487 to container port 3000
* And bind mount the working directory to the container path `/code`
* Start the main app.js and check outside container
    ![](https://i.imgur.com/cFTHcYJ.png)

## 6. Prepack node modules or install the node modules after container started
* If you install node packages and pack them into container image, the image would be heavy, but you can call the package functions directly. (Heavy image but first time run faster)
* If you wnat to use the latest node packages while you start the container, you have to package the `package.json` and call `npm install` during the container start. (Light image but run slow while first time install node packages)

## 7. If the dependencies check is fine, then write the Dockerfile
* Refer to https://docs.docker.com/get-started/02_our_app/


How to update the container image ?
===

## Basic instructions
* Show images
    * Refer to [Docker Image](https://docs.docker.com/engine/reference/commandline/images/)
    ```shell=
    docker images
    ```

* Write Dockerfile
    * Refer to the [Dockerfile reference](https://docs.docker.com/engine/reference/builder/)

* Build Docker image
    * Refer to [Docker Build](https://docs.docker.com/engine/reference/commandline/build/) document.
    ```shell=
    docker build -t <tag_name> . --no-cache
    ```

## Let your `kubectl` command control the EKS