|
| 1 | +# Hosting and Launching a Web Application with Docker on AWS EC2 Instance 🚀 |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This guide provides a step-by-step process to host and launch a web application using Docker on an AWS EC2 instance. |
| 6 | + |
| 7 | +## Prerequisites 📋 |
| 8 | + |
| 9 | +- An AWS account |
| 10 | +- Basic knowledge of AWS and Docker |
| 11 | +- AWS CLI installed and configured |
| 12 | +- SSH access to your EC2 instance |
| 13 | + |
| 14 | +## Step-by-Step Guide 🛠️ |
| 15 | + |
| 16 | +### Step 1: Launch an EC2 Instance 🚀 |
| 17 | + |
| 18 | +1. **Log in to the AWS Management Console**: |
| 19 | +  |
| 20 | + |
| 21 | +2. **Navigate to the EC2 Dashboard** and click "Launch Instance": |
| 22 | +  |
| 23 | + |
| 24 | +3. **Choose an Amazon Machine Image (AMI)**: Select the latest Ubuntu AMI. |
| 25 | +  |
| 26 | + |
| 27 | +4. **Choose an Instance Type**: Select t2.micro (eligible for the free tier). |
| 28 | +  |
| 29 | + |
| 30 | +5. **Configure Instance Details**: Keep the default settings. |
| 31 | +  |
| 32 | + |
| 33 | +6. **Add Storage**: Keep the default settings. |
| 34 | +  |
| 35 | + |
| 36 | +7. **Add Tags**: Add a tag with Key = `Name` and Value = `Docker-WebApp`. |
| 37 | +  |
| 38 | + |
| 39 | +8. **Configure Security Group**: |
| 40 | + - Create a new security group. |
| 41 | + - Add rules to allow SSH (port 22) and HTTP (port 80). |
| 42 | +  |
| 43 | + |
| 44 | +9. **Review and Launch**: Click "Launch" and create a new key pair or use an existing one. Download the key pair. |
| 45 | +  |
| 46 | + |
| 47 | +### Step 2: Connect to Your EC2 Instance 🔗 |
| 48 | + |
| 49 | +1. **Open your terminal** and navigate to the directory containing your key pair file. |
| 50 | + |
| 51 | +2. **Change the permissions of the key pair file**: |
| 52 | + |
| 53 | + ```sh |
| 54 | + chmod 400 your-key-pair.pem |
| 55 | + |
| 56 | +3. **Connect to your instance:** |
| 57 | + ```sh |
| 58 | + ssh -i "your-key-pair.pem" ubuntu@your-ec2-public-dns |
| 59 | +
|
| 60 | +### Step 3: Install Docker on EC2 Instance 🐳 |
| 61 | +
|
| 62 | +1. **Update the package database:** |
| 63 | + ```sh |
| 64 | + sudo apt-get update |
| 65 | + |
| 66 | +2. **Install Docker:** |
| 67 | + ```sh |
| 68 | + sudo apt-get install -y docker.io |
| 69 | +
|
| 70 | +3. **Start Docker and enable it to start on boot:** |
| 71 | + ```sh |
| 72 | + sudo systemctl start docker |
| 73 | + sudo systemctl enable docker |
| 74 | + |
| 75 | +4. **Verify Docker installation:" |
| 76 | + ```sh |
| 77 | + docker --version |
| 78 | +
|
| 79 | +### Step 4: Set Up Your Web Application 📦 |
| 80 | +1. **Clone your web application repository (replace the URL with your repository):** |
| 81 | + ```sh |
| 82 | + git clone https://github.com/your-username/your-webapp.git |
| 83 | + cd your-webapp |
| 84 | +
|
| 85 | +2. **Create a Dockerfile in the root of your project (if not already present):** |
| 86 | + ```dockerfile |
| 87 | + FROM node:14 |
| 88 | +
|
| 89 | + WORKDIR /app |
| 90 | +
|
| 91 | + COPY package*.json ./ |
| 92 | +
|
| 93 | + RUN npm install |
| 94 | +
|
| 95 | + COPY . . |
| 96 | +
|
| 97 | + EXPOSE 3000 |
| 98 | +
|
| 99 | + CMD ["npm", "start"] |
| 100 | + ``` |
| 101 | +3. **Build the Docker image:** |
| 102 | + ```sh |
| 103 | + docker build -t your-webapp . |
| 104 | +
|
| 105 | +4. **Run the Docker container:** |
| 106 | + ```sh |
| 107 | + docker run -d -p 80:3000 your-webapp |
| 108 | +
|
| 109 | +### Step 5: Access Your Web Application 🌐 |
| 110 | +Open a web browser and navigate to your EC2 instance's public DNS (IPv4) address. |
| 111 | +Your web application should be running and accessible on port 80. |
| 112 | +
|
| 113 | +### Conclusion 🎉 |
| 114 | +You have successfully hosted and launched your web application using Docker on an AWS EC2 instance. Your application is now live and accessible via the internet. |
| 115 | +
|
| 116 | +
|
| 117 | + |
0 commit comments