Containerization for beginners: Running a nodeJS application using Docker
Tags: node, javascript, docker, webdev
Published: 2021-10-11 15:10:26

There are a few questions that would come up when seeing the title. 
1. "Hey, why would I run my nodeJS application in a container? I can use node server, nodemon or n number of live servers available"

2. "When would I have to run it as a container?"

The answer is pretty simple if you're planning to not go through the tedious docker documentation out there.

#### Collaboration

We mostly work as a team. Our peers install and work with dependencies, pulling from the repository and getting the code running in our development server is usually cumbersome. Dependencies may be missing, system requirements need to be met, storage issues, version control, quite a lot of other obstacles. Here comes the containers!!

#### Isolation

Applications running in containers are isolated from each other and have their own storage and environments. We all would've gone through issues in production which cannot be captured in our development environment. Containers solve this right away.


## Steps to start a NodeJS container

**Pre-requisites: Docker desktop or docker-engine needs to be installed in the local machine**

A sample NodeJS app that listens in port 3000 is being taken here to run in a container using [Docker](https://docs.docker.com/) engine.

```
// app.js

const express = require('express')
const mongoose = require('mongoose');

const app = express();

app.get('/', (req, res) => {
   res.send('Yayyy!! Containers are a piece of cake');
});

app.listen(3000, () => {
   console.log('Container running with db connection');
});

```

### STEP 1: Create a Dockerfile

Create a file named `Dockerfile` without any file extensions with the following content.

```
FROM node:14

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 3000

CMD [ "node", "app.js" ]
```

Try figuring out what the above steps mean. They represent layers of a docker image. It is nothing but telling the Docker engine what procedure to follow when building the image. Wait a minute, what do you mean by an **image** ? Got you covered :wink:

[Docker images](https://docs.docker.com/engine/reference/commandline/images/) can be explained as blueprints to run an application in a container, just like how blueprints of a building help us to navigate through a block. 



### STEP 2: Run the docker engine

Open docker desktop or just run the `docker` command in the terminal (on Linux systems) to start the docker engine.



### STEP 3: Build the docker image

Navigate to the root directory of our application in the terminal. Run the following command to build the docker image of our application.

```
docker build .
```

The `.` in the end is just pointing to the root directory where our `Dockerfile` is placed. This command would download the base `node` image from the [Docker Hub](https://hub.docker.com/) or otherwise called a container registry. The above command would build the application container image which is to be run in a container.

After successfully building the image, the terminal presents us with a docker image id - something similar to this:
```
=> => writing image sha256:d8e42706********9
```

`d8e42706********9` is the image id here.



### STEP 4: Run the docker container

The last step is to run the container using the image id we have obtained. Run the following command to start our application in a container.

```
docker run -p 3000:3000 d8e42706********9
```

This would route the exposed port 3000 in the container to port 3000 in the local machine. Navigate to `localhost:3000` in your browser and check for the response from the server

**Yayyy!! Containers are a piece of cake** 

:cake: :cake: