Hosting Static Website With Docker Container in AWS EC-2

Smit Dharaiya
4 min readJul 24, 2020

Web hosting is a service that allows organizations and individuals to post a website or web page onto the Internet.So There are many ways to host our website But here i am hosting my static website with Docker Container in AWS Ec-2 Instance.So First We will go through What is Docker and Amazon EC-2.

Amazon EC2 :

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable computing capacity — literally, servers in Amazon’s data centers — that you use to build and host your software systems.

What is Docker ?

In general, docker is a tool designed to create, run and deploy your application with the help of containers.

Now , what the “containers” are?

Containers are running instance of docker images.

docker images are the immutable/frozen file which is a snapshot of containers and are generated by the build command in docker.

So in Simple Language we can say that image is the class and container is the instance of that class.

What is Docker engine ?

Docker engine is a part of Docker which creates and run docker containers.

Basic Docker Commands :

  • Docker version: docker-version
  • Docker compose version: docker-compose-version
  • Docker machine version: docker-machine -version
  • Verify docker is running: docker run hello-world
  • List running docker containers: docker ps
  • List stopped docker containers: docker ps -a
  • List images: docker images

Docker Installation :

So Here I am Providing Links for Mac and Windows :

Steps for Hosting a Website :

1.First Open the Docker and Go to the folder where we have all files.

2. We have to create a Docker File without any extention and write as follows :

Docker File

Here we are using nginx Server and Whatever File We have in current Directory all files got copied into the html folder of container.

3. Now We have to Build the Docker Image as Follows :

$ docker build -t dharaiyasmit/my-simple-webapp

Here dharaiyasmit is the Image name and my-simple-webapp is the Tag.

After Creation you can check it by following Command :

$ docker images

You Get Output Like this :

4. Now We have to Run This Image using Following Command :

$ docker run -d -name dharaiya -p 80:80 dharaiyasmit/simple-webapp

So here dharaiya is the container Name.

You can check the container is running or not using following Command.

$ docker ps

5. Now Run following command to Create Repository in Docker like Github and all files will be copied in that Repository.

$ docker push dharaiyasmit/simple-webapp

6. Now We will Create a EC2 instance in AWS.So Steps for Creating Ec2 instance are :

  • First login to AWS console.
  • Search for services known as EC-2.
  • In the EC-2 dashboard click on “Launch Instance”.
  • Choose an AMI according to your configuration.
  • Choose the Instance type and configure it according to your requirement.
  • Configure the storage according to your usage.
  • Leave the “add tags” as default for this only.
  • In Security group click Add Rule. after this in Type field add Http,SSH and Https and leave everything as else it is.
  • Click review your EC-2 instance and then click launch.
  • After launch, it will ask for key-value pair. Add your key name and download the key pair.
  • To access the Ec-2 from your PC download Putty.
  • Then open PuttyGen to generate Privte key.
  • Then open Putty and add you Ec-2 Instance public Ipv4. And then go tho Connection–>SSH–>Auth and Browse the key that you made in the previous step.
  • and then click Yes on prompt.
  • In user type ec2-user.

7. First We have to go to root account and Then We have to install Docker in the instance using following command

$ yum install docker

8. To start the service of Docker Run following Command

$ service docker start

9. Now Open the Docker and Go to that Repository then go to Tags where You can find the text like ,

docker pull dharaiyasmit/simple-webapp:latest

Copy this Text and paste it to the Ec2 instance cli.

But We have to replace pull by Run.

So our final Command is like ,

$ docker run dharaiyasmit/simple-webapp:latest

10. Finally We have Successfully Host our static Website. to check that copy the Public Ip of EC-2 Instance and search it to Browser where You can see your Static Website Content.

--

--