Build your First Node.js Application with Docker - blackgem

W E L C O M E

https://i.imgur.com/fEamA3G.png

Friday, April 7, 2023

Build your First Node.js Application with Docker


In our previous post we learned how to deploy a web app using k8s, now we are going to actually build the web app on Docker. Let's learn together.

In this section we will deploy a static web app, for now, no forms, no APIs, just a simple informative web app. For this we need to have a website running locally if you will.

Pre-Requisites

  • Docker Hub account and Docker Installed in your computer. 
  • Nodejs 
  • Docker

Create the Node.js app

Lets build a home for all the content we are about to create.

sudo mkdir docker_demoapp

sudo chown info docker_demoapp

And in this directory we will create a package.json file 

{ "name": "docker_webapp", "version": "1.0.0", "description": "Node.js WebApp on Docker", "author": "Black Gem ", "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "express": "^4.16.1" } }

Then we generate our package-lock.json by running npm install. This file will be copied to our Docker application.
Next, let's make a server.js file that will define a web app using the Express.js framework:

'use strict'; const express = require('express'); // Constants const PORT = 8080; const HOST = '0.0.0.0'; // App const app = express(); app.get('/', (req, res) => { res.send('Blackgem says Welcome'); }); app.listen(PORT, HOST, () => { console.log(`Running on http://${HOST}:${PORT}`); });

 

Now that we have our server ready, we will create our docker file.

Docker File

Create an empty file called Dockerfile:

sudo touch Dockerfile

sudo chown info Dockerfile

I opened in VS Code because it has a lot of support for Docker but you can decide which one to use.

Code Explained:

1. First we define from what image we want to build from. Here we will use the latest LTS (long term support) version 16 of node available from the Docker Hub.

2. Next we create a directory to hold the application code inside the image, this will be the working directory for your application.

3. This image comes with Node.js and npm already installed so the next thing we need to do is to install your app dependencies using the npm binary.

4. To bundle your app's source code inside the Docker image, use the COPY instruction

5. Our app binds to port 8080 so we will use the EXPOSE instruction to have it mapped by the docker daemon.

6. Define the command to run the app using CMD which defines the runtime. Here, we will use node server.js to start our server.

To get more information about Node.js please visit their website where you can check guides like this to keep practicing. 

Final Code:

FROM node:16 # 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 # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --omit=dev # Bundle app source COPY . . EXPOSE 8080 CMD [ "node", "server.js" ]

 


Next, we need to create our .dockerignore file : this will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.

sudo nano .dockerignore 
node_modules
npm-debug.log
Save.

Your directory should look now like this:



Finally we build our image. Please not that the command -t will allow us to tag our image so it's easier to find it later in the docker images.

Please note that for this step you need to have a docker hub account so you can add your username, in my case is blackgem.

docker build . -t blackgem/node-webapp

This is the final output and as you can see, every step that we detailed above is being processed when we run this command.


Now our image will be listed by Docker:


Let's run the image:

sudo docker run -p 49160:8080 -d blackgem/node-webapp



And now you can see your webapp in localhost:49160

To test your app:

sudo docker ps




And just like that we have our first node.js app up and running with Docker. Let me know if you find this useful ♥♦

No comments:

Post a Comment