Host Web Server Using Docker On AWS EC2 & Local: DevOps Project

Sourin
4 min readSep 15, 2023

For organisations and developers, being able to swiftly deploy and maintain web servers is crucial in today’s digital environment. With its scalable and dependable architecture, Amazon Web Services (AWS) has become the go-to platform for hosting applications and services in the cloud. Additionally, Docker has completely changed how we package and run apps when it comes to containerization.

What you will learn:

  • Basic server using Express.js
  • Docker Image Creation
  • Pushing Images to Docker Hub
  • How to run Containers on Local machine and AWS EC2

Source Code (Give it a Star ⭐):

My GitHub

Step 1: Creating Node.js Server and Running Local

First of all we will initialize our project using npm init -y . Then do npm install express .

We will write the code below in index.js file.

Then on the terminal we will write node index.js , it will run our server on our local machine.

Go to localhost:3000 on your browser, you will see this:

Change the URL to localhost:3000/about , it will take you to the about page, that we have written in the code. You will see this:

STEP 2: Creating Dockerfile and Docker Image

  • Create a file on the same folder named Dockerfile and write the following code:

Go to terminal and write docker build -t <Image-Name:tag> . , you can choose Image-Name and tag according to your wish, it will create the docker image. Creating Dockerfile look like this on the terminal:

Extra Step: Running on Local Machine (You can jump to Step 3)

  • Run docker images to view docker images present on your local machine:(Notice the first one, ignore rest of the images for this time being)
  • Run docker run -p 3000:3000 <Your-Image-name:tag> , it will run container for you. Then go to localhost:3000 on your browser to view the pages.

Step 3: Uploading to Docker Hub

  • For this step, you must have account in Docker hub and also you must be signed in with that account from your local machine.
  • Run these two commands and do not foget to replace

docker tag <your-image-name:tag> <your-docker-hub-username>/<repository-name:tag>

docker push <your-docker-hub-username>/<repository-name:tag>

Step 4: Connect to AWS EC2 & Download Docker Image There

  • If you don’t know how to create an EC2 instance and connect it to local machine, watch this video.
  • Pull (Dowload) the Docker image from Docker Hub using docker pull rylephoenix/my-node-server:v1 . (Here I am showing you with my own docker hub image-name and tag, I suggest you to do it with your own Docker Hub Image with correct tag-name that you have pushed in Step 3). It will look something like this:
  • You can view dowloaded images in your docker by running docker images . It will look like this (I have so many other images, ignore them, just notice the first one)

Step 5: Running Docker Container and Hosting on EC2

  • Run docker run -d — name node_app -p 8080:3000 <Container-Id> , it will run your container in the background.
  • Run docker ps -a to see all the containers running on your docker.
  • Congratulations🎉. We have successfully deployed a server on AWS EC2 instance using Docker Container.
  • Make sure you have Port range 8080 inbound rule up, in your AWS EC2 instance security groups, as we had deployed the container on port 8080.

Check on the Browser

  • Copy IPv4 Public address of your EC2 instance(shown below), then go to your browser and type: <IPv4-Address:Port> eg: http://16.171.145.78:8080/
  • You will see this, the same thing we saw when we ran this on local machine:

If you face any problem, you can always hit me up here :)

--

--