Prerequisites
- An AWS Account with an active EC2 instance (Ubuntu 22.04 recommended)
- A security group that allows inbound traffic on ports 22 (SSH), 8080, 8081, 8082 (HTTP)
- A Docker Hub Account (Sign up at Docker Hub)
- An SSH key to connect to your EC2 instance
Step 1: Set Up an EC2 Instance & Install Docker
- Launch an EC2 instance (Ubuntu 22.04 recommended).
- Connect to the instance via SSH:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
- Update the system:
sudo apt update && sudo apt upgrade -y
- Install necessary dependencies:
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Add Docker’s repository:
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Install Docker:
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io
- Verify Docker installation:
docker --version
- Enable Docker to run without sudo (optional):
sudo usermod -aG docker $USER
newgrp docker
Note: If you enable this, log out and log back in for changes to take effect.
Step 2: Create a Web Application
- Create a project directory:
mkdir docker-project && cd docker-project
- Create a folder for web files:
mkdir web
-
Create an
index.html
file:
nano web/index.html
- Add the following content:
<html>
<head><title>My Docker App</title></head>
<body><h1>Hello from Docker!</h1></body>
</html>
Step 3: Create a Dockerfile
-
Create the
Dockerfile
:
nano Dockerfile
- Add the following:
FROM nginx:latest
COPY web /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
- Build the Docker image:
docker build -t my-web-app .
- Run the container:
docker run -d -p 8080:80 my-web-app
-
Test in browser:
Visit
http://your-ec2-public-ip:8080
You should see: "Hello from Docker!"
Step 4: Install Docker Compose
- Download the latest Docker Compose binary:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Apply execute permissions:
sudo chmod +x /usr/local/bin/docker-compose
- Verify installation:
docker-compose --version
Step 5: Create a Docker Compose File
-
Create
docker-compose.yml
:
nano docker-compose.yml
- Add the following content:
version: '3.8'
services:
web:
build: .
ports:
- "8081:80"
depends_on:
- db
db:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: testdb
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3307:3306"
- Run Docker Compose:
docker-compose up -d
- Verify running containers:
docker ps
Step 6: Push the Image to Docker Hub
- Log in to Docker Hub:
docker login
- Tag the image:
docker tag my-web-app your-dockerhub-username/my-web-app:latest
- Push the image:
docker push your-dockerhub-username/my-web-app:latest
Step 7: Deploy on a New EC2 Instance
- Launch a new Ubuntu EC2 instance.
- SSH into the new instance:
ssh -i your-key.pem ubuntu@new-ec2-public-ip
- Install Docker:
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io
- Log in to Docker Hub:
docker login
- Pull the image:
docker pull your-dockerhub-username/my-web-app:latest
- Run the container:
docker run -d -p 8082:80 your-dockerhub-username/my-web-app:latest
-
Test in browser:
Visit
http://new-ec2-public-ip:8082
Deliverables
- Dockerfile
- docker-compose.yml
- Screenshot of the web app running inside the container
This guide ensures a complete hands-on learning experience, covering Docker installation, building images, running containers, Docker Compose, pushing to Docker Hub, and deploying on a new server.