Open In App

Web Page Hosting in Docker Container Using AWS

Improve
Improve
Like Article
Like
Save
Share
Report

The operating system (OS) libraries and dependencies required to run the application source code which is not reliant on the underlying operating system (OS) included in the Dockerfile, which is a standardized, executable component. Programmers may design, distribute, launch, run, upgrade, and manage containers using the open-source platform Docker.

What is Docker?

Docker is a powerful tool for packaging and deploying applications, and AWS provides a number of services that make it easy to host and run Docker containers in the cloud. In this article, we will discuss how to use Docker, AWS EC2, Apache2, Git, and GitHub to deploy a static website or web page on a Docker container. We will cover the steps involved in creating an AWS EC2 instance, installing and configuring Docker, and deploying a static website using Apache2. By the end of this article, you will have a solid understanding of how to use Docker and AWS to host a static website or web page on the AWS Cloud.

How Docker Works?

Docker is a free-to-use, open-source vessel-operation platform that provides tons of tools and serviceability to make, test, and emplace operations. You can produce packaged, insulated, and platform-independent holders with all the libraries and dependencies pre-built. This will allow you to develop and partake in operations fluently.

Virtualization vs Containerization

Virtual Machines vs Docker

A Virtual Machine (VM) is a virtual terrain that functions as a virtual computer system with its CPU, memory, network interface, and storehouse, created on a physical tackle system (located out or on-premises). Software called a hypervisor separates the machines from the hardware and vittles them meetly so they can be used by the VM.

Why Use Docker?

Docker is the most preferable and trustable platform for running the application in the form of containers. Following are some reasons to use Docker. A Docker container contains essential features dependencies, libraries, and runtime environments which are most important for the application to containerize. Because of this advantage, you can run your docker container in different environments like DEV, STAGE, and PROD without worrying about the conflict that will arise.

Docker containers are highly isolated. The main use of isolation is that it prevents conflicts between two different applications and makes sure that the data is mixed up with each other.

When To Use Docker?

Docker has a wide range of use cases some of which are as mentioned following.

  1. Microservices
  2. Continuous Integration and Continuous Deployment (CI/CD)
  3. Scalability and Load Balancing
  4. Application Packaging and Distribution

Steps For Web Page Hosting on Docker Container using AWS

Step 1: Click on EC2 service.

AWS Console

Step 2: Click on the Launch Instance.

launch instance

Step 3: Assign Name and tags. In this case, we are assigning it “Webserver”

name instance

Step 4: Choose an Amazon Machine image. For this article, we will be using “Amazon Linux”.

choose ami

Step 5: Choose the Instance Type as per your requirement and budget. For this, we will be proceeding with the free tier “t2.micro”

instance type

 

Step 6: Create a new Key Pair and Type the Name “linux1″.

key pair

Step 7: Create the Security Group.

  • Select – Create a new security group 
  • Security group name = sg-webserver
  • Allow HTTPS or HTTP traffic from the internet

security group

Step 8: Configure the Storage. 

config storage

Step 9: Click on Launch Instance.

launch

Getting Started With Docker on Amazon Linux EC2

Step 1: Command for root access.

$ sudo su
sudo access

 

Step 2: Update the Amazon Linux instance.

# yum update -y
update instance

 

Step 3: Install the Docker

# yum install docker -y
install docker

 

Step 4: Check the Docker version.

# docker --version
check docker version

 

Step 5: Start the docker service.

# service docker start
start docker

 

Step 6: Check docker service status is active or still inactive

# service docker status
docker status

 

Step 7: Search the AmazonLinux Image from Docker Hub

# docker search amazonlinux 

Step 8: Now Pull the Docker Image from the DockerHub or you can build yoou own image by using Dockerfile.

# docker pull amazonlinux

Step 9: Check the Images

# docker images

Step 10: Run the container with existence image and expose port 80.

# docker run -td --name 
webserver -p 80:80 amazonlinux
docker commands

 

Step 11: Check whether the port is open or not.

# docker port webserver

Step 12: Execute the Command and you will enter it into the Docker container Terminal.

# docker exec -it 
webserver /bin/bash
docker exec

 

Step 13: Update the container.

 # yum update -y
update container

 

Step 14: Install the Apache2 web server.

# yum install httpd -y

Step 15: Open the HTML directory to deploy the HTML(index.html) file.

# cd var/www/html
opening dir

 

Step 16: Run these commands for configuration.

# /usr/sbin/httpd
# vi /root/.bashrc               
        --- press "esc key" --> :wq --> "enter key"

Create the index.html file in the HTML directory.

# vi index.html

Example: 

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
    body{
      background-color: seagreen;
  }
  h1{
    margin-left: 400px;
  }
  h2{
    margin-left: 350px;
  }
  p{
   margin-left: 350px;
   font-size: 20px;
}
</style>
<title>Web Page Hosting on Docker Container</title>
</head>
<body>
  <h1>GEEKSFORGEEKS - Web Page Hosting on Docker Container</h1><br>
  <h2>Technologies Used - Docker, AWS-EC2, Apache2, Git & Github</h2><br>
  <p> In this ARTICLE I deploy the static website in docker container using AWS Instance
  <br> <br>
      Step :- <br>
    1.Launch the EC2 Instance in AWS <br>
    2.Install the Docker in Instance <br>
    3.Run the container in Docker <br>
    4.Configure the Web Server Apache2 in container <br>
    5.Deploy the code in web server <br>
  </p>
</body>
</html>


Press "esc key" --> :wq --> "enter key"

Step 17: Copy the IP Address from AWS EC2 Instance.

Public IP address

Step 18: Paste the IP Address in the Browser URL

IP address

Now we can see the website is hosted in Docker Container.

ooutput

Conclusion

In conclusion, we can install Docker on our own machines also but before installing the Docker you need to download some packages that are required to docker, and may it will affect your machine’s working performance and occupy a lot of space for that. So we can avoid this problem by installing the docker on virtual machines which are provided by the Amazon cloud in that virtual machine you can install the docker and deploy your web page in the form of containers.

FAQs On Host Web Page AWS Docker

1. Can I host a website in a Docker container?

Answer:

Yes you can host your website in the Docker in the form of containers.

2 . Is Docker good for hosting?

Answer:

Docker is the best platform to host any type of application because of its advantages.



Last Updated : 28 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads