AWS Builder Center

Deploying a Simple Web Application with Docker and Amazon EC2

Learn how to containerize a simple web application with Docker and deploy it on Amazon EC2. This guide covers the basic workflow from building a Docker image to running and accessing the application on AWS.

Deploying a Simple Web Application with Docker and Amazon EC2

Cloud computing makes it easier to deploy applications without managing physical servers. AWS provides many services that help developers build, deploy, and scale applications.
In this article, we will learn how to deploy a simple web application using Docker and Amazon EC2.

What We Will Build

Our deployment flow will look like this:
Web Application → Docker Image → Amazon EC2 → Running Container → Web Browser
We will:
  1. Create a simple web application.
  2. Create a Dockerfile.
  3. Build a Docker image.
  4. Run the application using Docker.
  5. Launch an Amazon EC2 instance.
  6. Install Docker on the EC2 instance.
  7. Run the application on EC2.
  8. Access the application from a browser.

What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) provides virtual servers in the AWS Cloud.
Instead of purchasing and maintaining a physical server, we can create an EC2 instance and use it to run our applications.
An EC2 instance can be used for:
  • Hosting websites
  • Running backend applications
  • Running APIs
  • Deploying Docker containers
  • Hosting development environments

What is Docker?

Docker allows us to package an application together with its required dependencies into a container.
A container provides a consistent environment for running an application.
For example:
1
2
3
4
5
6
7
8
9
Application
+
Dependencies
+
Configuration
↓
Docker Image
↓
Docker Container
This makes it easier to move an application from a developer's computer to a cloud server.

Step 1: Create a Simple Web Application

For demonstration, we can create a simple HTML page.
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<head>
<title>AWS Docker Demo</title>
</head>
<body>
<h1>Hello from AWS EC2!</h1>
<p>This application is running inside a Docker container.</p>
</body>
</html>
Save the file as:
1
index.html

Step 2: Create a Dockerfile

Next, create a file named Dockerfile.
1
2
3
4
5
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
Here is what each line does:
  • FROM nginx:alpine uses a lightweight NGINX image.
  • COPY copies our HTML file into the NGINX web directory.
  • EXPOSE 80 indicates that the application uses port 80.

Step 3: Build the Docker Image

Open a terminal inside the project directory and run:
1
docker build -t aws-docker-demo .
Docker reads the Dockerfile and creates an image.
We can check the image using:
1
docker images

Step 4: Run the Container Locally

Run:
1
docker run -d -p 8080:80 aws-docker-demo
The command maps:
1
Computer Port 8080 → Container Port 80
Now open:
1
http://localhost:8080
The web page should appear in the browser.

Step 5: Launch an EC2 Instance

Now we can deploy the same application to AWS.
From the AWS Management Console:
  1. Open Amazon EC2.
  2. Choose Launch Instance.
  3. Select an appropriate Amazon Machine Image (AMI).
  4. Choose an instance type suitable for your workload.
  5. Configure the required network and security settings.
  6. Create or select a key pair.
  7. Launch the instance.
For a learning project, always choose resources appropriate for your needs and remember to stop or terminate resources when they are no longer required.

Step 6: Connect to the EC2 Instance

After the instance is running, connect to it using SSH or the connection method provided by the AWS console.
Once connected, update the system packages and install Docker according to the instructions for your selected operating system.
After Docker is installed, verify it:
1
docker --version

Step 7: Get the Application onto EC2

There are several ways to transfer an application to EC2.
For example, we can:
  • Clone the project from GitHub.
  • Copy files using SCP.
  • Pull a Docker image from Amazon ECR or another container registry.
For a production workflow, using a container registry such as Amazon Elastic Container Registry (Amazon ECR) is a common approach.

Conclusion

Docker and Amazon EC2 provide a simple way to understand cloud-based application deployment.
The basic workflow is:
Build → Containerize → Deploy → Run → Access
Starting with a small application is a good way to learn the fundamentals before moving to more advanced architectures involving services such as Amazon ECR, Application Load Balancer, Amazon ECS, or Amazon EKS.
The most important concept is not just running an application on a cloud server, but understanding how the different components work together to deliver an application to users.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article