Tuesday, January 3, 2023

How to Dockerise a simple Web Application / Host on a Nginx Server

  

How to Dockerise a simple Web Application / Host on a Nginx Server

 Nginx, stylized as NGINX or nginx or NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. This can be used to host web Applications.

In this lab exercise, you will learn how to create a simple web App, Create an Nginx container and deploy that App into the container. And view the results on your browser

Prerequisites: 

Install Docker


Step 1: Create the web App(HTML)

Log into your docker  machine

Create the Html file using Vi Editor

$ vi index.html

 

<!DOCTYPE html>

<html lang="en">

<head>

<title>Smashing Champions Web App!</title>

</head>

<body >

We are champions

we are champions!

</body>

</html>

Copy the Above code and paste on the editor. You can edit the body section as u please

Step 2: Create Dockerfile
open vi editor and copy the below code

$ vi Dockerfile
#FROM is the base image for which we will run our application
FROM nginx:latest

# Copy files and directories from the application
COPY index.html /usr/share/nginx/html

# Tell Docker we are going to use this port
EXPOSE 80





The above Docker file will create a base image from centos6 operating system, install nginx web server on it, then Copy the index.html file we created into "/usr/share/nginx/html" folder of the centos container. The image will be exposed in Port 80

Step 3: Create the docker image: You can tag it with -t , tag it any name you want. but we will use webapp

$ docker build -t webapp .
 
List docker images
$ docker images
You can see the base image we specified in the Dockerfile (centos), and the image created from it (webapp)

Step 4: Lets run our image
$ docker run -itd -p 8888:80 IMAGE_ID

Place your image id in place of  highlighted yellow


Step 5: Check if image is running

$ docker ps -a


Step 6: Go to your ip address:8888 (make sure u open this port in your security group

No comments:

Post a Comment

How to Deploy to Kubernetes using Argo CD and GitOps

  How to Deploy to Kubernetes using Argo CD and GitOps Using Kubernetes to deploy your application can provide significant infrastructural a...