Deploy a simple “Hello World” Node.js web app from a GitHub repository, containerize it with Docker, and run it on an AWS EC2 instance.
Here’s a simple beginner-level diagram for this lab:
GitHub Repo
|
| git clone
v
AWS EC2
+---------------+
| Docker Engine |
| Node.js App |
+---------------+
|
Port 3000
|
v
Browser
┌───────────────────────┐
│ 1️⃣ Local Machine │
│ aws-docker-lab/ │
│ ┌───────────────┐ │
│ │ server.js │ │
│ │ package.json │ │
│ │ Dockerfile │ │
│ └───────────────┘ │
│ │
│ git push / GitHub Web │
└───────────┬──────────┘
│
▼
┌─────────────────────────────┐
│ 2️⃣ GitHub Repository │
│ https://github.com/ │
│ awsrmmustansarjavaid/ │
│ aws-docker-lab │
│ ┌───────────────┐ │
│ │ server.js │ │
│ │ package.json │ │
│ │ Dockerfile │ │
│ └───────────────┘ │
└───────────┬─────────────────┘
│ git clone
▼
┌─────────────────────────────┐
│ 3️⃣ AWS EC2 Instance │
│ Amazon Linux 2023 │
│ ┌───────────────┐ │
│ │ ~/aws-docker-lab │ │
│ │ server.js │ │
│ │ package.json │ │
│ │ Dockerfile │ │
│ └───────────────┘ │
│ sudo docker build -t aws-docker-lab . │
│ sudo docker run -d -p 3000:3000 aws-docker-lab │
└───────────┬─────────────────┘
│ Port 3000
▼
┌─────────────────────────────┐
│ 4️⃣ Browser / Public Access │
│ http://<EC2_PUBLIC_IP>:3000│
│ Output: "Hello from AWS EC2 + Docker!" │
└─────────────────────────────┘
-
You create the project folder with server.js, package.json, and Dockerfile.
-
Push to GitHub via GitHub Web (or git push if Git installed locally).
-
Central place to store your code.
-
EC2 will pull the latest version from here.
-
Launch EC2 → install Docker → clone repo.
-
Build Docker image → run container mapping port 3000.
-
Use EC2 public IP and port 3000 to access your Node.js app.
-
You should see: "Hello from AWS EC2 + Docker!".
-
AWS account (with permissions to create EC2, Security Groups, and IAM user if needed)
-
GitHub account
-
Local machine with Git installed
-
Basic terminal knowledge
-
Go to GitHub → log in.
-
Click New Repository (green button on the top right).
-
Set repository Name → aws-docker-lab.
-
Optional: add a Description → “Simple Node.js Docker Lab”.
-
Repository Type → Public or Private.
-
Do not check “Initialize this repository with a README” (or you can, it’s optional but helpful).
-
Click Create Repository.
-
Open File Explorer (Windows) or Finder (Mac) or terminal.
-
Create a folder called aws-docker-lab.
mkdir aws-docker-lab
cd aws-docker-lab
mkdir ~/aws-docker-lab
cd ~/aws-docker-lab
Inside that folder, create a file called server.js.
- right-click → New → Text Document → rename to server.js
touch server.js
- Open the file in a code editor (VS Code, Sublime Text, Notepad++).
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello from AWS EC2 + Docker!');
});
app.listen(port, () => {
console.log(`App running at http://localhost:${port}`);
});
This file tells Node.js what dependencies the app needs.
- Create a file called package.json in the same folder.
{
"name": "aws-docker-lab",
"version": "1.0.0",
"main": "server.js",
"dependencies": {
"express": "^4.18.2"
},
"scripts": {
"start": "node server.js"
}
}
- Open terminal in your project folder.
git init
git add .
git commit -m "Initial Node.js app"
-
Go to your newly created repository page.
-
Click Add File → Upload Files.
-
On your computer, open the aws-docker-lab folder.
-
Drag and drop all files inside the folder (not the folder itself) into GitHub.
- So, upload server.js, package.json, and later Dockerfile.
-
Scroll down, enter a commit message like Initial Node.js app.
-
Click Commit changes.
You do not need to upload the folder itself, only the contents of the folder.
aws-docker-lab/
├── server.js
├── package.json
- Later, you can add the Dockerfile the same way: Add File → Upload Files → Commit.
Your folder structure should look like this:
aws-docker-lab/
├── server.js
├── package.json
At this point, your Node.js app is ready locally.
Next, you will push it to GitHub, so EC2 can pull it and run it with Docker.
In your project folder, create a file named Dockerfile (no extension).
# Use Node.js official image
FROM node:18
# Set working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy all source files
COPY . .
# Expose port
EXPOSE 3000
# Command to run app
CMD ["npm", "start"]
git add Dockerfile
git commit -m "Add Dockerfile"
git push
aws-docker-lab/
├── server.js
├── package.json
├── Dockerfile
-
Each file should have a GitHub icon and clickable filename.
-
You now have a complete repo ready for EC2 to clone.
-
Log in to AWS Management Console .
-
Navigate to EC2 → Instances → Launch Instances.
-
Choose AMI:
-
Amazon Linux 2023 (recommended) OR
-
Ubuntu 22.04
-
-
Choose Instance Type → t2.micro (free tier eligible).
-
Configure Key Pair:
- Create new key pair → download .pem file → save securely.
-
Configure Security Group:
-
Add SSH → port 22 → Source: My IP
-
Add Custom TCP Rule → port 3000 → Source: 0.0.0.0/0 (for testing)
-
-
Click Launch Instance → wait for instance to start.
-
Note Public IPv4 address of the instance.
- Open terminal on your machine.
sudo chmod 400 my-key.pem
-
Connect:
-
Amazon Linux:
ssh -i my-key.pem ec2-user@<EC2_PUBLIC_IP>
- Ubuntu:
ssh -i my-key.pem ubuntu@<EC2_PUBLIC_IP>
Amazon Linux commands:
sudo dnf update -y
sudo dnf install -y docker
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user
docker --version
Docker version 24.0.5, build ffff...
cd ~
sudo dnf update -y
sudo dnf install git -y
- dnf is the package manager for Amazon Linux 2023.
git --version
git version 2.40.1
Now you can clone your repo. Use your personal GitHub repo (the one you actually own):
git clone https://github.com/<your-username>/aws-docker-lab.git
Do not use https://github.com/aws-docker-lab/aws-docker-lab.git because that’s not your repo.
⚠️ Replace with your GitHub username.
cd aws-docker-lab
ls -l
or
cd ~/aws-docker-lab
server.js
package.json
Dockerfile
Build the Docker image with a name aws-docker-lab:
sudo docker build -t aws-docker-lab .
-
-t aws-docker-lab → names the image
-
. → current directory (Dockerfile location)
Successfully built <image-id>
Successfully tagged aws-docker-lab:latest
sudo docker run -d -p 3000:3000 aws-docker-lab
-
-d → run in detached mode (runs in background)
-
-p 3000:3000 → maps container port 3000 to EC2 port 3000
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 aws-docker-lab "node server.js" 2 minutes ago Up 2 minutes 0.0.0.0:3000->3000/tcp goofy_hopper
-
Go to AWS Console → EC2 → Security Groups.
-
Select the security group attached to your instance.
| Type | Protocol | Port Range | Source |
|---|---|---|---|
| SSH | TCP | 22 | My IP |
| Custom TCP | TCP | 3000 | 0.0.0.0/0 |
- Save rules.
This allows your browser to access the Node.js app from anywhere
http://<EC2_PUBLIC_IP>:3000
Hello from AWS EC2 + Docker!
sudo docker ps
sudo docker stop <container-id>
sudo docker start <container-id>
sudo docker rm <container-id>
docker rmi aws-docker-lab
- GitHub → EC2 → Docker → Browser ✅

