ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


Docker - Setup a Node.js development environment

Life is short, enjoy life.

After using Docker to build the development environment that make me feel more happy and less painful.

Be happy use Docker 🐳.

Same with Python Node.js also can running Node.js on local volume or build all the source code inside the container image.

1. Create a folder call docker_dev_nodejs.

2. Create package.json

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "childofcode <docker@childofcode.com>",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "axios": "^0.20.0"
  }
}

We can use npm command on local machine to generate the package.json. If you local machine does't install Node.js and npm you can manually create a package.json.

3. Create a Dockerfile with below configuration

FROM node:14

WORKDIR /node  
COPY package.json ./

RUN npm install  
WORKDIR /node/app

COPY . .

CMD ["npm", "start"]  

FROM node:14 Download Node.js 14 version Images from docker hub'

WORKDIR /node Create a main working directory inside docker container '

If you get Module not found issue probably is module directory issue.
we can either modify project structure moving every editable code inside a folder let's say src, or update your Dockerfile to move node_modules a directory upper.

COPY package.json ./ Copy package.json inside docker main working directory'

RUN npm install Install npm package Module on main working directory.

WORKDIR /node/app Create a place to store the source code

COPY . . Copy the entire project to build

CMD ["npm", "start"] Execute npm command to install package module

4.Build the image by Dockerfile

docker build -t node_env .

-t is give a tag name call node_env

. don't miss the dot is mean the current location

5.Create a Node.js Scripts

Create a node.js script calling app.js with code below. We will using a axios module to GET the nodejs.org page.

const axios = require('axios');

 axios.get('https://nodejs.org/en/')
  .then(function (response) {
    // handle success
    console.log(response.status);
    console.log(response.data);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
    console.log("Done");
  }); 

6 Run the Docker container Image

docker run -it --rm node_env

you will see '200' and nodejs.org HTML return on the terminal.
We can copy all our script on build Docker images and just run the images .

7.Running Node.js Script Using Docker with Mount a local directory

For development stage more flexible method is running our code on local directory and only using the Node.js and module on the Docker container image.

docker run --rm -v /Users/jack/Desktop/code/docker/docker_dev_nodejs/:/node/app node_env  node app.js  

--rm option will clean up your container after use.

-v flag Use volumes by Docker containers.

/Users/YourWorkSpace/docker_dev_nodejs/ is the local directory

/usr/src/app is the docker container directory

node app.js execute the Node.js script

Run the command on terminal you should see status code 200 and HTML return if success.