Dockerize a Node.js app with VS Code

Dockerize a Node.js app with VS Code

  • 6/26/2019
  • 279 views

Hello dear coder, welcome to my tech articles series dedicated to Node.js and Docker. Hope you enjoy it!

Problem:

You have never used Docker before, and you are a JavaScript developer. You want to understand how to use containers together with Node. Lets’ go!

1. Setup

In this tutorial, I will be using my favourite editor, Visual Studio Code. You will also need Nodejs and Docker installed. In my case, I’m using Windows, so I got Docker for Windows from here.

Following command creates a new folder with name test-node-app, then creates a npm package file and opens the folder in VS Code.

mkdir test-node-app && cd test-node-app && npm init -y && code .

Also please install express to create a server.

npm i express

 

2. Create a basic Node app

Open package.json file and modify it like this:

{
 "name": "test-node-app",
 "version": "1.0.0",
 "description": "",
 "main": "server.js",
 "scripts": {
   "start": "node server.js",
   "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [],
 "author": "",
 "license": "ISC",
 "dependencies": {
   "express": "^4.17.1"
  }
}

 

Now add a new record with name server.js:

const express = require("express");

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();

app.get("/", (req, res) => {
  res.send("Hello from Node.js app \n");
});

app.listen(PORT, HOST, () => {
    console.log(`Running on http://${HOST}:${PORT}`);
});

The super basic basic basic app is ready. Now let’s test it locally. Run this command inside app folder:

npm start

You should see following in the console and will be able to access the server on the link http://localhost:8080/

 

3. Prepare Docker file

To build our image, we need a docker file. Create a file with name Dockerfile

touch Dockerfile

And paste this inside:

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 8080

CMD [ "npm", "start" ]

This file tells Docker to download Node.js of version 8, create a directory for the app, copy their package.json files, run npm install for node_modules, says which port this app wants to use and finally runs it with npm start.

Uhhh, a lot yeah? 😮

We will also need a file called .dockerignore. Create and paste following inside:

node_modules
npm-debug.log

 

4. Build a Docker image

First of all, build our image with this command.

docker build -t test-node-app .

Wait until it finishes and now we can see the new image created.

docker images

If you are using VS Code, you can install a very lovely Docker extension. It adds a button to side panel which opens Docker explorer. Here you can see all available images and containers. From there you can run, stop and remove containers which are pretty useful.

If you want to remove an image run this:

docker rmi <image id>

 

5. Run Docker container (finally)

Time to run a container based on our image (our application in an isolated environment). The important thing is that we need to tell docker how to map the port. Now we run our container and map 8080 port to 49165.

docker run -p 49165:8080 -d test-node-app

Which means we can now access the app by this URL http://localhost:49165/

There is another handy command to see all running containers:

docker ps

You can stop container by right click in VS Code Docker Extension or by this command:

docker stop <container id>

Now if you run docker ps Again you will not see your container running. If you want to see stopped containers, you need to add —all flag to the command like this:

docker ps -a

If you still have some questions you can easily refer to the official documentation here.

Related Articles