Write a Dockerfile to build a Docker Image for Node JS Web app, And  Attach a docker volume to the selected docker image, and map a port to it.

Write a Dockerfile to build a Docker Image for Node JS Web app, And Attach a docker volume to the selected docker image, and map a port to it.

#devops #git #linux #github #docker #dockerhub #aws #ec2

Table of contents

No heading

No headings in the article.

  1. What is docker ?

Docker is a popular platform for building, shipping, and running applications in containers. It provides a lightweight and flexible way to package an application and its dependencies into a single container that can be run on any system that has Docker installed. Docker allows developers to easily create, deploy, and manage applications in a consistent and reproducible way, making it easier to develop and scale complex applications. Docker also provides features for managing containers, such as networking, storage, and security, and integrates with other tools and technologies commonly used in the software development lifecycle.

Here are some key benefits and use cases of using Docker:

Application deployment: Docker provides a lightweight and portable way to package an application and its dependencies into a single container that can be run on any system that has Docker installed. This makes it easier to deploy and manage applications across different environments, such as on-premises, cloud, or hybrid.

Microservices architecture: Docker is well-suited for microservices architectures, where applications are broken down into smaller, independently deployable components. Docker containers enable each microservice to run in its own isolated environment, making it easier to manage and scale complex applications.

Continuous integration and delivery: Docker can be used to build and test applications in containers, providing a consistent and reproducible environment for each step in the development process. This makes it easier to automate the process of building, testing, and deploying applications, enabling faster and more reliable releases.

Cloud migration: Docker can be used to migrate applications to the cloud, making it easier to move applications between different cloud providers or on-premises environments. Docker containers provide a consistent and portable environment for running applications, making it easier to manage and scale applications in the cloud.

DevOps automation: Docker can be integrated with other DevOps tools and technologies, such as Kubernetes, Jenkins, and Ansible, to automate various aspects of the software development lifecycle. This can help improve collaboration between development and operations teams, streamline application delivery, and improve overall efficiency.

Overall, Docker provides a flexible and scalable platform for building, shipping, and running applications in containers, enabling developers to easily create, deploy, and manage complex applications across different environments and technologies.

What is Docker Image ?

A Docker image is a lightweight and standalone executable package that includes everything needed to run an application, including the application code, runtime, libraries, and dependencies. It is built from a Dockerfile and can be used to create Docker containers, which run in their own isolated environment. Docker images are portable and can be deployed across different systems and platforms, making it easy to scale and manage applications.

What is Docker Container ?

A Docker container is a lightweight, standalone, and executable package that contains everything needed to run an application, including the application code, runtime, libraries, and dependencies, and runs in an isolated environment. It is created from a Docker image and can be started, stopped, and deleted as needed. Docker containers are portable and can run on any system that has Docker installed, making it easy to deploy and manage applications across different environments.

What is Docker Volume ?

A Docker volume is a way to store and manage persistent data outside of a container. It is a directory on the host file system or in a remote storage system that can be mounted into a container, allowing the container to read and write data to the volume. Docker volumes enable data to persist beyond the lifetime of a container, allowing it to be reused by other containers or even on different hosts, and can be backed up and restored separately from containers. They are useful for managing shared data, optimizing performance, and providing an additional layer of data protection.

Use Case of Docker Volume ?

Sharing data between containers: Volumes allow multiple containers to share the same data, making it easier to manage and update. This is particularly useful for microservices architectures, where data needs to be shared between multiple services.

Data persistence: Docker volumes enable data to persist beyond the lifetime of a container, allowing it to be reused by other containers or even on different hosts. This is useful for databases, file storage, and other applications that require persistent data.

Backup and restore: Docker volumes can be backed up and restored separately from containers, providing an additional layer of data protection. This is useful for applications that store critical data.

Performance optimization: Volumes can be optimized for performance by using local storage or high-speed network storage, depending on the application requirements. This can improve application performance and scalability.

Overall, Docker volumes provide a flexible and scalable way to manage persistent data in containerized applications, enabling developers to easily share, store, and manage data across multiple containers and environments.

What is Docker Port Mapping ?

Docker port mapping is the process of mapping a port on the host machine to a port in a Docker container, allowing external systems to access the services running inside the container by connecting to the mapped port on the host machine. It is a useful feature for exposing services running inside containers to the outside world and allows multiple containers to run on the same host machine without port conflicts.

Here We going to see one Example with step by step explanation of the Above mentioned information :

Step 1 : AWS Management Console

Create a AWS account with free trail membership.

host 1 EC2 instance which has contains UBUNTU OS.

And access the terminal using pem key or AWS Systems Manager Service.

After that update the instance using the following command .

$ sudo apt-get update && sudo apt-get upgrade

Step 2: Docker Installation :

Then, install the docker using following commands

$ sudo apt-get install docker.io -y

$ sudo usermod -aG docker $USER

Step3 : Git/Github

Firstly, install the Git using the following command

$ sudo apt-get install git

Then, clone the code from Github Repository using the following command

$ git clone https://github.com/shivarajkonnur297/reddit-clone-k8s-ingress.git

Then, e;nter into the cloned repository and by using following command :

$ cd reddit-clone-k8s-ingress

Step 4 : Dockerfile

Then, Write a Dockerfile using Vi editor and paste the following code:

$ vim Dockerfile
# Use an official Node.js runtime as a parent image
FROM node:14-alpine as build
# Set the working directory to /app
WORKDIR /app
# Copy the rest of the application code to the working directory
COPY . .
# Install Node.js dependencies
RUN npm install
# Build the ReactJS application
RUN npm run build
# Use an official Nginx runtime as a parent image
FROM nginx:stable-alpine
# Copy the ReactJS application from the build stage to the Nginx web server directory
COPY --from=build /app/build /usr/share/nginx/html
# Expose port 80 for incoming traffic
EXPOSE 80
# add docker volume to it
VOLUME [ "/app/data" ]
#Start Nginx and keep the container running in the foreground
CMD ["nginx", "-g", "daemon off;"]

Save the file using [esc] :wq!

Then Build the image using DockerFile using the following command :

$ sudo docker build –t your-dockerhub-username/node-web-app:v1 .

And then run the Built image in container using following command :

$ sudo docker run –it –d –p 80:80 your-dockerhub-username/node-web-app:v1

step 5 : dockerhub

If the container Running smoothly,login into dockerhub and push the docker image into your DockerHub using following command :

$ sudo docker login
$ sudo docker push your-dockerhub-username/node-web-app:v1

#please replace the dockerhub username by yours#

That’s all for today , Thank you for learning .