# Devops Project 1

How to build Docker image for any Application

![Docker - Visual Studio Marketplace](https://ms-azuretools.gallerycdn.vsassets.io/extensions/ms-azuretools/vscode-docker/1.24.0/1677187109445/Microsoft.VisualStudio.Services.Icons.Default align="left")

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/](https://nodejs.org/en/)
    

**Step 1: Install The Docker Application and Node.js**

/[https://www.docker.com/products/docker-desktop/](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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677761822347/95cd39b9-903a-463f-8fe5-7ccb488df2d6.png align="center")

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

```plaintext
npm init -y
```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677762050436/789ffc62-642f-4967-93d4-df1c391fc6b8.png align="center")

**Step 3:**

Create the Index.js file

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

```plaintext
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).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677757470080/59ef0a2d-9253-4452-b606-83b8d647e10b.png align="center")

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

```javascript

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/](http://localhost:3000/)

```powershell
node index.js
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677763049096/4553decb-4dd4-4539-9b3b-6f2fbab9234b.png align="center")

**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**

```powershell
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.

**<mark>Note: "." is used for the Current directory. This is used in the code below.</mark>**

**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.

```plaintext
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

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

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

```powershell
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.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677837837478/c57fd80b-3674-42e1-9deb-17b9ce688c56.png align="center")

```powershell
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677838238563/f0b79b60-5982-472f-8230-03919dcb2b48.png align="center")

**Step 11:** Hit the URL [http://localhost:3000/](http://localhost:3000/) and the application is working fine.

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

```powershell
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.

```powershell
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677839057813/f28febef-1378-4fbd-b48d-bd203e2d2417.png align="center")

**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.

```plaintext
docker -v
```
