Dockerized A Node.js Application
Tags: showdev, slimcoder
Published: 2020-05-13 08:01:43

---
title: Dockerized A Node.js Application
published: true
description: In this article, we will discuss docker and how to dockerized a Node.js Application.
tags: showdev, slimcoder
cover_image: https://miro.medium.com/max/1400/1*7YOoDx_nbhPgTSYierFTNg.png
---

## Table Of Contents
* [JavaScript](#chapter-1)
* [Node.js](#chapter-2)
* [Express](#chapter-3)
* [Simple Express.js Server](#chapter-4)
* [Docker](#chapter-5)
* [Dockerized Express.js Server](#chapter-6)
* [Final Note](#chapter-7)

### JavaScript <a name="chapter-1"></a>

JavaScript is a multi-purpose, multi-paradigm, single-threaded programming language used on client and server-side.

### Node.js <a name="chapter-2"></a>

Node.js is a JavaScript runtime environment, It is responsible for running JavaScript on the server side.

You can download Node.js from [here](https://nodejs.org/en/download/).

Node.js setup will install itself and [npm](https://www.npmjs.com/) (node package manager) which is use from installing dependencies.

### Express.js <a name="chapter-3"></a>

Express.js is the Node.js framework which is responsible for creating REST API's.

### Simple Express.js Server <a name="chapter-4"></a>

Now we need to create a simple Express.js:

For creating a server we need to create a new folder then inside that folder open your favorite IDE.

Now run this command (make sure you have node already installed).

        npm init // This will initialize npm within directory
        npm install express --save //This will install Express.js and save in package.json dependencies 
        mkdir index.js //Create a new file

Now add this code in index.js file:

        const express = require('express'); // Require express.js framework
        
        const app = express(); //Create a app container 
        
        app.get('/', function (req, res) { // Add a route (API)
        res.send('Hello World')
        }); 
        
        app.listen(3000); // Listen on a port

Now your express server is ready now run this:

        node index.js //This will run index.js file

Now open your browser in tab add this url **http://localhost:3000**.

Yahoo! Your first Express.js app is ready (Happy Hacking).

### Docker <a name="chapter-5"></a>

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

This means that Docker makes our life easy by creating a container now we will create a container of our Node.js application but first download Docker from [here](https://www.docker.com/products/docker-desktop).

### Dockerized Express.js Server <a name="chapter-6"></a>

Now we need to Dockerized our Node Application so add a **Dockerfile** in our project folder. 

And add these commands inside **Dockerfile**.

       # This file is the main docker file configurations

       # Official Node JS runtime as a parent image 
       FROM node:9-alpine

       # Set the working directory to .
       WORKDIR .

      # Install app dependencies
      # A wildcard is used to ensure both package.json AND package- 
      lock.json are copied
      # where available (npm@5+)
     COPY package*.json ./

      # Install any needed packages
      RUN npm install

      # Bundle app source
      COPY . ./

      # Make port 3000 available to the world outside this container
      EXPOSE 3000

      # Run app.js when the container launches
      CMD ["npm", "start"]

Now run **docker build --tag node:latest** and then **docker run --publish 8000:8080 --detach --name bb node:latest** to build and run docker image locally

Yahoo! you first dockerized application is ready to server on **http://localhost:3000**.

### Final Note <a name="chapter-7"></a>

I have created a repository all codes which we have written is inside of this repository feel free to look into the codebase.

{% github learnwithgeeks/node-docker-session %}

**Happy Hacking**