Docker expose, Learn to host website in container, AWS Ec2-instance.

Fenil Vaghasiya
4 min readJul 16, 2022

Hello Friends,

In this article, I have explain how deployed website in container and enable the container port and ec2-instance port.

First, you have to launch ec2-instance. Please follow below steps:

Step:1 Click on Launch instance button.

Step:2 Enter a name of instance like Docker.

Figure: To enter name of instance

Step :3 Select Amazon Linux AMI (Amazon Machine Image).

Figure: To select the AMI

Step: 4 Instance type: t2.micro. (Free trial eligible).

Figure: To select the instance tyoe

Step:5 Select New key pair. Enter an name: docker pair and click the create key pair.

Figure: To create new key pair

Step 6: In network settings, Please allow HTTPS and HTTP ports and source type: anywhere

Figure: To add inbound rules

Step 7: Click on Launch Instance.

Here you can seen, our ec2-instace is created.

Figure: To display list of ec2-instances

Now connect ec2-instance via SSH. Copy the public IP address of ec2-instance.

Command: sudo ssh -i <filename>.pem ec2-user@ip

Figure: To connect ec2-instance

You have need to grant access to the root user and update the system.

Command: sudo su

Command: yum update -y

Figure: To update the server

First you have Install the docker engine on ec2-instance.

Command: yum install docker -y

Figure: To install docker

After install docker engine, check whether docker engine start or not. If not first you must start docker engine.

Command: service docker status

Command: service docker start

Figure: To start docker engine

Yeah, Successfully docker engine started….

Now, You have created one container and name is “fenilcontainer” and allow the port of this container. So that map port of the container with port of the ec2-instance.

Command: docker run -td — name <container name> -p 80:80 ubuntu

Here, left end side 80 denoted ec2-instance port and right end side 80 denoted container port.

Figure: To create an container

You are suppose to check container details like container id, name etc.

Command: docker ps -a

Figure: To check container details

To check which port you have expose in particular container.

Command: docker port <container name>

Figure: To check which port allow to container

Now you have go inside the container.

Command: docker exec -it <container name> /bin/bash

Figure: To go inside container

So, here you are in the container and in this container install apache server and start the apache server.

Command: apt-get update

Figure: To update the container

Command: apt-get install apache2

Figure: To install apache server

Command: service apache2 start

Figure: To start the apache server

Now you can copy public IP address of ec2-instance and past the any browser.

You can see the default page of apache server.

Figure: To display default web page

If you want to change or add any content of this page then please follow below command.

Command: cd /var/www/html

Command: ls

Command: echo “Welcome, How are you ??…” > index.html

Figure: To edit the default index.html file

Now refresh the browser and see what happen. Yeah its change it…

Figure: To display edited part

After that stop container and delete the container.

Figure: To delete container

That’s it !!

Thank you….

--

--

Fenil Vaghasiya

AWS Community Builder | 2x AWS Certified | 1x Azure Certified