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:
123456789Application +Dependencies +Configuration ↓Docker Image ↓Docker ContainerThis 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.
12345678910<!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:
1index.htmlStep 2: Create a Dockerfile
Next, create a file named
Dockerfile.12345FROM nginx:alpineCOPY index.html /usr/share/nginx/html/index.htmlEXPOSE 80Here 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:
1docker build -t aws-docker-demo .Docker reads the Dockerfile and creates an image.
We can check the image using:
1docker imagesStep 4: Run the Container Locally
Run:
1docker run -d -p 8080:80 aws-docker-demoThe command maps:
1Computer Port 8080 → Container Port 80Now open:
1http://localhost:8080The 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:
1docker --versionStep 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.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article