Step-by-Step: Spring Boot + Docker + AWS EC2 Deployment In the last video, we saw manual upload of a Spring Boot JAR to AWS EC2. The main disadvantage was that the EC2 instance had to mirror local setup — Java, Maven, etc., had to be installed manually. Today, we’ll deploy Spring Boot using Docker → Docker Hub → EC2. This method removes the need to install Java/Maven on EC2 and ensures consistent deployment. Step 1: Create Spring Boot Application (Local) Create a simple Spring Boot REST API: @RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String hello() { return "Spring Boot Application Deployed via Docker Successfully"; } } Test locally: http://localhost:8080/api/hello ✅ Application works locally. Step 2: Install Docker on Local Machine Install Docker Desktop (Windows/Mac) or Docker Engine (Linux). Verify installation: docker --version Example output: Docker version 28.3.3, build 980b856 Step 3: Create Dockerfile for Spring Boot In your project root, create a Dockerfile with: Use official OpenJDK 21 image FROM openjdk:21-jdk If not available, use eclipse-temurin:21-jdk WORKDIR /app COPY target/AwsEc2Manul-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","app.jar"] This converts your JAR into a Docker image. Step 4: Build Docker Image (Local) From project root: docker build -t springboot-ec2-demo . Note: If OpenJDK image is unavailable, switch to eclipse-temurin:21-jdk. Verify the image: docker images Example output: REPOSITORY TAG IMAGE ID CREATED SIZE springboot-ec2-demo latest b702f4540015 About a minute 712MB ✅ JAR converted to Docker image (containerized). Step 5: Create Docker Hub Account (if not already) Docker Hub is an online registry to store images. From EC2, we can pull images directly. Other clouds: GCP → Artifact Registry, AWS → ECR. Example username: prabhakaranskg Step 6: Tag Docker Image for Docker Hub docker tag springboot-ec2-demo prabhakaranskg/springboot-ec2-demo:latest Format: (dockerhub-username)/(image-name):(tag) Step 7: Push Docker Image to Docker Hub Login: docker login Push image: docker push prabhakaranskg/springboot-ec2-demo:latest ✅ Image is now on Docker Hub. Step 8: Prepare EC2 Instance Launch EC2 (Ubuntu 22.04, t3.micro) Security group: Protocol Port Source SSH 22 My IP HTTP 80 0.0.0.0/0 Custom TCP 8080 0.0.0.0/0 Download key pair (aws-ec2-docker.pem) Connect via SSH: ssh -i "D:\Youtube\AWS\aws-ec2-docker.pem" [email protected] Windows PEM permission fix (PowerShell): icacls "D:\Youtube\AWS\aws-ec2-docker.pem" /reset icacls "D:\Youtube\AWS\aws-ec2-docker.pem" /inheritance:r icacls "D:\Youtube\AWS\aws-ec2-docker.pem" /grant:r "$($env:USERNAME):R" icacls "D:\Youtube\AWS\aws-ec2-docker.pem" /remove "Authenticated Users" "Users" "Administrators" "SYSTEM" ✅ Successfully connected to EC2. Step 9: Install Docker on EC2 sudo apt update sudo apt install docker.io -y sudo systemctl enable docker sudo systemctl start docker sudo usermod -aG docker ubuntu docker --version Example output: Docker version 28.2.2, build 28.2.2-0ubuntu1~24.04.1 Step 10: Pull Docker Image on EC2 docker pull prabhakaranskg/springboot-ec2-demo:latest ✅ Image downloaded from Docker Hub. Step 11: Run Docker Container on EC2 docker run -d -p 8080:8080 prabhakaranskg/springboot-ec2-demo:latest Flags explanation: -d → Detached mode (background) -p 8080:8080 → Map EC2 port 8080 to container port 8080 Optional: name container docker run -d -p 8080:8080 --name springboot-app prabhakaranskg/springboot-ec2-demo:latest Stop or remove old containers if needed: docker stop (container_id) docker rm (container_id) docker pull prabhakaranskg/springboot-ec2-demo:latest Step 12: Test the Application Open browser or Postman: http://(EC2_PUBLIC_IP):8080/api/hello http://18.60.157.146:8080/hello Response: Spring Boot Application Deployed in EC2 Successfully Using Docker ✅ Application running successfully on EC2. Step 13: Verify Docker on EC2 docker ps # shows running containers docker logs (container_id) # check logs Step 14: Optional: Stop/Remove Container docker stop (container_id) docker rm (container_id) ✅ Summary / Workflow Local machine: Build Spring Boot JAR Create Dockerfile Build Docker image Tag and push to Docker Hub EC2 machine: Install Docker Pull image from Docker Hub Run container Your Spring Boot application is now accessible via EC2 public IP. Thanks for watching! Next, we’ll see a simple CI/CD pipeline to automate deployment of Spring Boot Docker applications on AWS EC2.