Dockerize NodeJs Application
Tags: docker, javascript, node, webdev
Published: 2022-05-04 11:01:33

**_> Docker is an open platform for developing, shipping, and running applications._**

 In other words, It enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

Lets look at practical example on how we can use Docker to ship our NodeJs app and all the development dependencies using Docker.
![container](https://media.giphy.com/media/6AFldi5xJQYIo/giphy.gif)


##### Prerequisite
🎯 Docker [download](https://docs.docker.com/desktop/windows/install/)
🎯 NodeJs [download](https://nodejs.org/en/)

##### ⚙️ Setup
Make sure your docker daemon is active. Click on the Docker Desktop Icon to start the Docker engine.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkdzac86i65qyp7vdvc6.PNG)
 
---

##### Check if docker is installed on your PC correctly
Run this `command` : docker --version
##### Result
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2ksv0d93cvn7mokajhy.PNG)


##### Dockerize NodeJS App

Let initiate our NodeJs Project using the `npm init -y` command
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdyqlq0zrj3ib70dkebe.PNG)

##### Install Project Dependencies
Install Express using `npm i express`, create index.js file and create a NodeJs Server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh93rlke7hdhj6lb9xg5.PNG)

##### Let's test our app
For simplicity sake, i edited the `package.json` file and fired the app using `npm start`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k618ze5564mvlxuil3i6.PNG)

#### Dockerizing your NodeJs App
Create a Dockerfile and Paste the below code
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44orp0bfhte5uuclxsuw.PNG)
 
##### Code explanation
```javascript
1. The FROM command tells Docker to use the node:14.17.0-alpine image as the base image for the Dockerfile.
2. WORKDIR /app tells Docker to set the working directory for the container to /app.
3. ADD package*.json ./ tells Docker to copy the package.json file into the container.
4. RUN npm install tells Docker to run npm install inside the container.
5. ADD index.js ./ tells Docker to copy the index.js file into the container.
6. EXPOSE 5050 tells Docker to expose the port 5050 on the container.
7. CMD [ "node", "index.js"] tells Docker to run node index.js inside the container. 
```

---

#### Let's Build our app
The `dockerized-app` is the name i gave my Docker Image that i want to build. You can use any descriptive name of your choice. The `.` is referring to the current folder.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qge26cqxloilbb45tu8y.PNG)

---

#### Let's check our built image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebjn2ooblyjnfkyr0oh2.PNG)

---

#### Next, we run our container from the images
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncobqjriisbjf3p6wuuq.PNG)

##### Explanation:
```javascript
1. The -d flag is used to run the container in the background
2. The -p flag is used to set the port that the container will listen on.
3. The port number 4001 is your localhost port and 5050 the port that you are making the request to i.e the Container port that we exposed earlier.
4. The name of the container is node-api
5. The dockerized-app image is the name of the image that you are going to run.
````
---

#### Check all running containers
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw7k6ppvan79cg2yt25v.PNG)

---

#### Lets test our exposed port of our running instance on browser
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp9qrcl0cua1irqlnqdq.PNG)


---

#### Inspect your running instance
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdood66ajc5mvyb5gg25.PNG)
    

---

#### Stop a running container
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7psjs2stwnrujkudhnyp.PNG)

---

#### Restart a running container
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/067booj845e2gtkdf8by.PNG)


---

#### Remove the running container (force remove)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmyshwj5tjq2j1kkdc2o.PNG)
 

#### Conclusion
I hope this post was helpful.
  
#### Resources
[TechWorld with Nana](https://www.youtube.com/watch?v=3c-iBn73dDE&t=7282s)
[Docker Amigoes](https://www.youtube.com/watch?v=p28piYY_wv8&t=10051s)
[Docker](https://www.docker.com/)