Devops Project 1

Devops Project 1

Table of contents

No heading

No headings in the article.

How to build Docker image for any Application

Docker - Visual Studio Marketplace

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

In this Project, we are going to Build a docker Image for Node.js Application. But going through this article, You can build Docker Images for any application.

Let's get started

Requirements:

  1. You Need to Install the Docker desktop Application and sign up for the same.

  2. You Need to install the Node.Js from here https://nodejs.org/en/

Step 1: Install The Docker Application and Node.js

/https://www.docker.com/products/docker-desktop/

Go to the above link and download the Desktop app and sign up/Register for the same. It should look like the below Image

Step 2: Go to VS code and go to terminal and write

npm init -y

hit enter and the package.JSON file will be created. Make the changes as shown below.

Step 3:

Create the Index.js file

In The next step Hit the below in the VS Code Terminal

npm i express

Go to express documentation and copy the hello world code and paste the same on the index.js file (For Your Convenience I have given the code below with some changes).

Step 4: Make some changes in the index.js file as shown in the below screenshot.


const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.json({
    "Hey":"NodeJS"
  })
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Step 5: Write the below command in the terminal, Hitting the below Command will start the server and the result is also shown in the below screenshot.

Hit this URL after hitting the below Command http://localhost:3000/

node index.js

Step 6: Go to your VS Code and add a new file and name it as

dockerfile

Step 7: Go to the VS code terminal and write the code as shown below

docker login

login to the docker with the correct credentials.

Step 7: Now go to your Docker file and paste the below code

Before that what does the below code means let's see that first

FROM: From where you want to take your base image (Base image is available on the docker hub)

WORKDIR: This is the working directory.

COPY: This is something you want to copy from source to destination.

Note: "." is used for the Current directory. This is used in the code below.

RUN: This is the RUN command used to bring back the node modules folders back.

EXPOSE: This is used for the port so we have used port 3000.

CMD: This is the command which will first be used to start the application.

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

Step 8: Write the below Command on the terminal.

Note: "." is mandatory

docker build -t riddhi2230/hey-nodejs:0.0.1.RELEASE .

Step 9: Hit the below command to check the list of containers

docker containes ls

Step 10: Hit the below Command to run your Container. Before that make sure your image is now available on your docker (Desktop App) as shown in the screenshot below.

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

You will now see the image as shown in the below screenshot.

Step 11: Hit the URL http://localhost:3000/ and the application is working fine.

Step 12: You can also stop the container with below command.

docker container stop 820

You can give any number to stop the container.

The application /container will be stopped.

Congratulations! You can create a REST API and at least one endpoint and also able to containerize it.

Now I want this image to be public. Hit the below command to public your Image.

docker push riddhi2230/hey-nodejs:0.0.1.RELEASE .

This will take my image and will push it onto the repository from where anybody can pull this off(as shown in the below screenshot). You might be in AWS, GOOGLE CLOUD or MICROSOFT AZURE.

Queries You may face.

Q.1) Facing Issues with Docker?

Answer: Go to Command-Line and write the below code, Make sure that the downloaded version is correct.

docker -v