Skip to main content

Command Palette

Search for a command to run...

Day 17 Task: Docker Project for DevOps Engineers

Published
2 min read
A

DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab

This is an exciting challenge! Let’s break down the steps to create a Dockerfile for a simple web application using Node.js. Here’s how you can do it:

Step 1: Set Up Your Node.js Application

  1. Create a new directory for your project:

     mkdir my-node-app
     cd my-node-app
    
  2. Initialize a new Node.js application:

     npm init -y
    
  3. Install Express (or any other framework you prefer):

     npm install express
    
  4. Create a simple web server in index.js:

     const express = require('express');
     const app = express();
     const PORT = process.env.PORT || 3000;
    
     app.get('/', (req, res) => {
         res.send('Hello, World!');
     });
    
     app.listen(PORT,'0.0.0.0', () => {
         console.log(`Server is running on http://localhost:${PORT}`);
     });
    

Step 2: Create the Dockerfile

Create a file named Dockerfile (no extension) in your project directory:

# Use the official Node.js image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your application files
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the application
CMD ["node", "index.js"]

Step 3: Build the Docker Image

Run the following command in the terminal from your project directory:

docker build -t simple-nodejs .

Step 4: Run the Docker Container

Once the image is built, run the container:

docker run -p 3000:3000 simple-nodejs

Step 5: Verify the Application

Open your web browser and go to http://localhost:3000. You should see "Hello, World!" displayed.

Step 6: Push the Image to a Repository

  1. Log in to Docker Hub:

     docker login
    
  2. Tag your image (replace yourusername with your Docker Hub username):

     docker tag simple-nodejs amitsinghs98/simple-nodejs
    
  3. Push the image:

     docker push amitsinghs98/simple-nodejs
    

Summary

You’ve successfully created a Dockerfile for a Node.js application, built the Docker image, run the container, verified it in the browser, and pushed the image to Docker Hub! 🎉

Feel free to share your experience on LinkedIn and tag your community! Happy learning! 😃

More from this blog

DevOps Projects and Blog

69 posts