Create a  crazy Node.js application using Docker

Create a crazy Node.js application using Docker

·

3 min read

Step 1: Launch an AWS EC2 instance from aws console with Ubuntu AMI & t2 micro and ssh to the particular instance using putty or EC2 instance connect

Step 2: Run the following commands to update the packages, install & start the docker engine

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

Step 3: Create a Node.js application in the Ubuntu instance create a directory and navigate to it.

mkdir my-node-app
cd my-node-app

Step 4: Create a file for the node.js application with the name index.js

touch index.js

Step 5: Write a node.js application in the index.js using an editor (vi, vim & nano )

const http = require('http');
const fs = require('fs');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    fs.readFile('index.html', (err, data) => {
      if (err) throw err;
      res.end(data);
    });
  } else if (req.url === '/submit') {
    let body = '';
    req.on('data', chunk => {
      body += chunk.toString();
    });
    req.on('end', () => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/html');
      res.end(`<h1>Form submitted:</h1><p>${body}</p>`);
    });

Step 6: Create an index.html file for a better UI experience for node.js applications in the web.

touch index.html

Copy & paste the following html code in index.html and make changes as required

<!DOCTYPE html>
<html>
  <head>
    <title>My Website</title>
  </head>
  <body>
    <h1>Welcome to My Website!</h1>
    <p>Please fill out this form:</p>
    <form>
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" required><br><br>

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" required><br><br>

      <label for="message">Message:</label>
      <textarea id="message" name="message" required></textarea><br><br>

      <input type="submit" value="Submit">
    </form>

    <p>Check out this image:</p>
    <img src="https://thumbs.dreamstime.com/z/person-pointing-their-fingers-person-pointing-their-fingers-bearded-man-points-hands-palms-something-below-smiling-253638222.jpg" alt="Person pointing their fingers">
  </body>
</html>

Step 7: Create a docker file mentioning the image with the latest or specific version, copy the necessary application files to the specific directory, and define the port which needs to be exposed

# Use an official Node.js runtime as a parent image
FROM node:12

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in package.json
RUN npm install

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

# Define environment variable
ENV NAME World

# Run app.js when the container launches
CMD ["node", "index.js"]

Step 8: Run the docker command to build the image using the docker file in my case docker file which is the present same working directory

docker build -t node-app .

Step 9: Create a docker container with a node-app image to run the application with the following docker command

docker run -p 3000:3000 -d node-app

Step 10: To test the node.js application open the browser and navigate to 'http://<EC2-instance-public-IP>:3000' make sure to allow the inbound rule 3000 custom tcp port in the Security group of your instance