Project: Host Static Website on Docker Container thru Dockerfile and Push Docker Image to Docker Hub (Normal)
[EC2 + Github + Docker]
Reference Videos: 1. https://youtu.be/uEfUxFnlxgM (for Normal)
2. https://youtu.be/eEU6gae494Y (thru Terraform)
Step 1: Create Public Repo in Docker Hub to store image & Github Repo for website files
Step 2: Create EC2 with 22,80 ports and install docker and create Dockerfile and run needed commands for httpd
Step 3: build the docker image from Dockerfile and push to docker hub
Step 4: Run the container from the above docker image which contains our needed website files
Step 5: check in browser for website with public IP of container running EC2
Note: Prepare all Terraform code for Infra setup and run Terraform commands to host website [thru Terraform]
sudo yum update -y
Sudo yum install git -y
sudo amazon-linux-extras install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
sudo usermod -a -G docker ec2-user
/var/run/docker.sock
sudo vi Dockerfile
docker build -t . nn-techmax
docker login --username xxxxxxx
docker tag nn-techmax xxxxxxxx/nn-techmax
docker push xxxxxxx/nn-techmax
docker run -d --name nn-container -p 80:80 xxxxxxxx/nn-techmax
Task-2 Thru Terraform
1. Create build_image.sh file with commands inside
2. Create my_password.txt file on Desktop with our dockerhub password inside it
3. Create Dockerfile same as in Task-1
4. Create ec2.tf file [# with vpc, subnet, azone, ec2, SG script]
5. Run the Terraform commands in integrated Terminal in VS Code
check the image in dockerhub and check website in browser
1. Dockerfile:
FROM amazonlinux:latest
RUN yum update -y && \
yum install -y httpd && \
yum search wget && \
yum install wget -y && \
yum install unzip -y
RUN cd /var/www/html
RUN wget https://github.com/azeezsalu/jupiter/archive/refs/heads/main.zip
RUN unzip main.zip
RUN cp -r jupiter-main/* /var/www/html/
RUN rm -rf jupiter-main main.zip
EXPOSE 80
ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]
2. build_image.sh :
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo systemctl enable docker
sudo usermod -a -G docker ec2-user
docker info
sudo vi Dockerfile
docker build -t nn-techmax .
docker login --username xxxxxxx
docker tag techmax xxxxxx/nn-techmax
docker push xxxxxx/nn-techmax
docker run -dp 80:80 --name nn-container xxxxxx/nn-techmax
3. ec2.tf :
provider "aws" {
region = "us-east-1"
profile = "nn-terraform"
}
resource "aws_default_vpc" "default_vpc" {
tags = {
Name = "default vpc"
}
}
data "aws_availability_zones" "available_zones" {}
resource "aws_default_subnet" "default_az1" {
availability_zone = data.aws_availability_zones.available_zones.names[0]
tags = {
Name = "default subnet"
}
}
resource "aws_security_group" "ec2_security_group" {
name = "docker server SG"
description = "allow access on ports 80 and 22"
vpc_id = aws_default_vpc.default_vpc.id
ingress {
description = "http access"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "ssh access"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "docker server sg"
}
}
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
resource "aws_instance" "ec2_instance" {
ami = data.aws_ami.amazon_linux_2.id
instance_type = "t2.micro"
subnet_id = aws_default_subnet.default_az1.id
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
key_name = "nar*****"
tags = {
Name = "docker server"
}
}
resource "null_resource" "name" {
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/Downloads/nar****.pem")
host = aws_instance.ec2_instance.public_ip
}
provisioner "file" {
source = "~/Downloads/my-dhub-password.txt"
destination = "/home/ec2-user/my-dhub-password.txt"
}
provisioner "file" {
source = "Dockerfile"
destination = "/home/ec2-user/Dockerfile"
}
provisioner "file" {
source = "techmax-docker-tf.sh"
destination = "/home/ec2-user/techmax-docker-tf.sh"
}
provisioner "remote-exec" {
inline = [
"sudo chmod +x /home/ec2-user/techmax-docker-tf.sh",
"sh /home/ec2-user/techmax-docker-tf.sh",
]
}
depends_on = [aws_instance.ec2_instance]
}
output "container_url" {
value = join("", ["http://", aws_instance.ec2_instance.public_dns])
}