Docker Compose File and Implementation of Containerizing a Node Application

In the ever-evolving landscape of software development, Docker has emerged as a game-changer, revolutionizing the way applications are deployed and managed. In this blog post, we'll delve into the practical aspects of Docker, exploring its significance, evolution, and key components. Whether you're a seasoned developer or just stepping into the world of containerization, this guide aims to demystify Docker and provide actionable insights for your journey. You can Find the Repo Here (Give it a start)




1. Setting Up Your Environment:

Before diving into Dockerizing your Node.js application, ensure you have Docker installed on your system. You can download and install Docker Desktop from the official Docker website.


2. Creating a Node.js Application:

Start by creating a simple Node.js application if you haven't already. You can use any text editor or an IDE of your choice. For example, let's create a basic index.js file:

var express = require("express");
var app = express();
app.get("/", function (req, res) {
  res.send("Hello World!");
});
app.listen(3000, function () {
  console.log("Example app listening on port 3000!");
});


3. Dockerfile Creation:

Create a Dockerfile in the root directory of your Node.js application. This file contains instructions for Docker to build your application's image

FROM node:slim
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD  node index.js


4. Building the Docker Image:

Open a terminal window, navigate to your application's directory, and run the following command to build your Docker image


docker build -t <YOUR_DOCKER_NAME>/hey-nodejs:0.0.1.RELEASE  .


5. Docker Compose File Creation:

Docker Compose is a tool for defining and running multi-container Docker applications. Create a docker-compose.yml file in your project directory

# docker-compose.yml
version: '3'
services:
  web:
    build: .
    ports:
      - '3000:3000'


6. Running Your Dockerized Application:

Now, use Docker Compose to start your application. Run the following command in your terminal

docker-compose up


7. Accessing Your Application:
Once your application is up and running, open your web browser and navigate to http://localhost:3000 to see your Node.js application running inside a Docker container


8. For Running the Container on the local system we can use:

docker container run -d -p 3000:3000 piyushmahajan1703/hey-nodejs:0.0.1.RELEASE


9. For Pushing Your image to the Docker Hub we can use:

docker push piyushmahajan1703/hey-nodejs:0.0.1.RELEASE


Conclusion:

Containerizing a Node.js application with Docker is a straightforward process that offers numerous benefits, including portability, scalability, and ease of deployment. By following these step-by-step instructions, you can seamlessly integrate Docker into your development workflow and leverage its power to streamline application deployment


Post a Comment

0 Comments