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:
- Create a simple web application.
- Create a Dockerfile.
- Build a Docker image.
- Run the application using Docker.
- Launch an Amazon EC2 instance.
- Install Docker on the EC2 instance.
- Run the application on EC2.
- 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
<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:alpineuses a lightweight NGINX image.COPYcopies our HTML file into the NGINX web directory.EXPOSE 80indicates 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:
- Open Amazon EC2.
- Choose Launch Instance.
- Select an appropriate Amazon Machine Image (AMI).
- Choose an instance type suitable for your workload.
- Configure the required network and security settings.
- Create or select a key pair.
- 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.
The deployment architecture can look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Developer
|
↓
GitHub / Source Code
|
↓
Docker Build
|
↓
Container Registry
|
↓
Amazon EC2
|
↓
Docker Container
|
↓
Users
Step 8: Run the Container on EC2
After the Docker image is available on the EC2 instance, run:
1
docker run -d -p 80:80 aws-docker-demo
The container is now running on the EC2 instance.
Step 9: Configure Network Access
To access the application from the internet, the EC2 instance needs appropriate network configuration.
The EC2 security group acts as a virtual firewall.
For a basic HTTP application, the required inbound rule can allow HTTP traffic on:
1
2
Port: 80
Protocol: TCP
Avoid opening unnecessary ports to the internet. Only allow the traffic required by your application.
Step 10: Access the Application
Find the public IP address or public DNS name of the EC2 instance.
Then open it in your browser:
1
http://<EC2-PUBLIC-IP>
If everything is configured correctly, the web application should be displayed.
Understanding the Complete Flow
The complete process is:
1
2
3
4
5
6
7
8
9
10
11
12
13
AWS Cloud
|
Amazon EC2
|
Docker Engine
|
Docker Container
|
NGINX
|
Web Application
|
Users
The main advantage of this approach is that Docker keeps the application environment consistent, while EC2 provides the compute infrastructure needed to run the application.
Important Security Considerations
When deploying applications on AWS, security should be considered from the beginning.
1. Do not expose unnecessary ports
Only allow required ports through the security group.
2. Protect SSH access
Do not unnecessarily expose SSH access to everyone. Restrict access whenever possible.
3. Protect credentials
Never store AWS access keys, passwords, or other secrets directly inside source code or Docker images.
4. Keep software updated
Regularly update the operating system, Docker images, and application dependencies.
5. Monitor your resources
AWS monitoring tools can help identify unusual resource usage and potential problems.
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.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article