# Docker Project for DevOps Engineers

### **What is Dockerfile**

A Dockerfile is a text file that contains a set of instructions for building a Docker image. The Dockerfile describes how to create a custom Docker image that includes all the necessary dependencies and configurations for running an application. The Dockerfile contains commands that are executed in sequence to build the image. Each command in the Dockerfile creates a new layer in the image.

Dockerfile is used as the source code to build Docker images, and it is highly configurable, allowing developers to customize the environment for their application. Dockerfile is typically used with the `docker build` command, which takes the Dockerfile as input and produces a Docker image as output. Once the image is built, it can be used to start containers that run the application.

Dockerfiles are commonly used to define and automate the build process for complex applications that require specific dependencies, configuration, and environment variables. They are a powerful tool for building consistent and reproducible Docker images that can be easily shared and deployed across different environments.

Let's talk about what we insert in ***Dockerfile***

1. `FROM`: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.
    
2. `RUN`: Executes a command in the image. This command is run during the image build process. For example, you might use the `RUN` command to install necessary packages or dependencies for your application.
    
3. `COPY`: Copies files from the host machine to the image. For example, you might use the `COPY` command to copy the files for your application into the image.
    
4. `ENV`: Sets an environment variable in the image. For example, you might use the `ENV` command to set a variable that holds the version of your application.
    
5. `EXPOSE`: Specifies the ports that should be exposed on the container. For example, you might use the `EXPOSE` command to specify that port 8000 should be exposed on the container.
    
6. `CMD`: Specifies the command that should be run when a container is created from the image. For example, you might use the `CMD` command to specify that your application should be started when the container is created.
    

Here’s an example of a simple Dockerfile that sets up a Node.js environment:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686232445126/8d694bcb-8771-48e0-9916-41a7505222f1.png align="center")

* `FROM` creates a layer from the `node:12.2.0-alpine` Docker image
    
* `WORKDIR` defines the working directory of a Docker container
    
* `COPY` adds files from the host machine to the image
    
* `RUN` installs necessary packages or dependencies like `npm` for the application
    
* `EXPOSE` specifies the port 8000 that should be exposed on the container
    
* `CMD` specifies what command to run within the container
    
* ### **Project:- Node Todo App**
    

![nystudio107 | Running Node.js in Docker for local development](https://nystudio107.com/img/blog/_1200x630_crop_center-center_82_none/run-your-node-js-apps-buildchains-via-docker.jpg?mtime=1599964647 align="left")

### **Task: Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)**

Clone the repository in your system using the `git clone` command, view the content of the repository & create a **Dockerfile** if not present or modify the present **Dockerfile.**

**Step 1:-** We can find the source code from the below GitHub location

[https://github.com/bharatdevops-bn4/node-todo-cicd.git](https://github.com/bharatdevops-bn4/node-todo-cicd.git)

**Step 2:-** Firstly need to create a separate directory to create a project so it will very easy to track all the changes we need to clone the repo by using the below command

```plaintext
git clone <Repo name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686236637852/55da24b7-a13d-49df-b83e-70a0480079aa.png align="center")

**Step 3:-** Now we need to create Dockerfile to write the set of instructions to run the code

**NOTE:-** Here we have already a docker file that case no need to create a again

```plaintext
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]
```

**Step 4:-** Now using the Docker file we can create an Docker image

```plaintext
docker build . -t <image_name>
# Ex:- sudo docker build . -t node_todo_app
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686236802582/805d9a34-772e-45f0-8a02-2f90b0c2352a.png align="center")

**Step 5:-** Now using the above docker image we can create a n number of docker containers

```plaintext
docker run -itd -p 8000:8000 node_todo_app
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686236910361/d6805e3a-a338-48e0-bf4f-cfeb1dbfc0b6.png align="center")

**Step 6:-** Now we need to search in the web browser along with the ipv4 address and port number

Note: Go to your ec2 instance security group and check whether the port `8000` is opened or not. If not, then add it to the inbound rules of the security group.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686237098047/8816952f-d1b0-4dc3-8b62-8ddf68199326.png align="center")

**Step 7:-** Now we need to push that image to the Docker Hub repo so everyone can use that same image. using the below url we can log in to Docker Hub

[**https://hub.docker.com/**](https://hub.docker.com/)

**Step 8:-** Now we need to log in to the docker hub from the Linux machine using docker hub conditionals

```plaintext
docker login
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686237388831/ffc780ee-b89c-4bda-b798-9405cd0db28f.png align="center")

**Step 9:-** Now we need to tag that image before pushing it to the docker hub

```plaintext
docker tag node_todo_app bharat3006/node_todo_app
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686237606285/e7e6bbcd-745e-43b2-ac14-c56bde2c4a79.png align="center")

**Step 10:-** Now we can push that image to the docker hub

```plaintext
docker push bharat3006/node_todo_app
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686237788343/77074b40-f3fd-49b0-a750-96d7fab8a040.png align="center")

**Step 11:-** Now we can see the docker image in the docker hub

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686237867562/cd3e4f22-6d2e-45bd-87b5-7ea4483d0b01.png align="center")

Congratulations! ✨🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉You have successfully installed Docker on your Ubuntu instance, added the current user to the Docker group, created a Docker container for the provided Node-Js demo application. Exposed port 8000 for everyone using IPv4 in the inbound rule of your instance's security group. Also pushed the Docker images to DockerHub.

***Thank you for reading!!!***  
**~Bhaarat**
