DEV Community

Cover image for Dockerize NodeJs Application
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Dockerize NodeJs Application

> 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

Prerequisite

🎯 Docker download
🎯 NodeJs download

⚙️ Setup

Make sure your docker daemon is active. Click on the Docker Desktop Icon to start the Docker engine.
Image description


Check if docker is installed on your PC correctly

Run this command : docker --version

Result

Image description

Dockerize NodeJS App

Let initiate our NodeJs Project using the npm init -y command
Image description

Install Project Dependencies

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

Image description

Let's test our app

For simplicity sake, i edited the package.json file and fired the app using npm start
Image description

Dockerizing your NodeJs App

Create a Dockerfile and Paste the below code
Image description

Code explanation
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. 
Enter fullscreen mode Exit fullscreen mode

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


Let's check our built image

Image description


Next, we run our container from the images

Image description

Explanation:
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.
Enter fullscreen mode Exit fullscreen mode

`

Check all running containers

Image description


Lets test our exposed port of our running instance on browser

Image description


Inspect your running instance

Image description


Stop a running container

Image description


Restart a running container

Image description


Remove the running container (force remove)

Image description

Conclusion

I hope this post was helpful.

Resources

TechWorld with Nana
Docker Amigoes
Docker

Top comments (2)

Collapse
 
zahakhussain profile image
Muhammad Zahak

Issue resolved?

Collapse
 
Sloan, the sloth mascot
Comment deleted