Dockerizing a Spring Boot App
Guide to Deploying to AWS EC2
As a associate system administrator I worked on Redhat Linux servers, including user management, permissions, services, and performance monitoring Automated routine administrative tasks using Bash scripting and cron jobs, reducing manual effort by ~30% I am aws certified sysops administrator and Google Certified Cloud Engineer. Determined to transition my career into cloud architect /Cloud Support role
This guide walks you through the process of containerizing a Spring Boot To-Do application, publishing the image to Docker Hub, and deploying it on an AWS EC2 instance.
By the end, you will have a fully functional application running in the cloud.
Prerequisites:
A Docker Hub account
An AWS account
Basic familiarity with the command line, Docker, and AWS EC2
Step 1: Clone the Repository :
Begin by obtaining the application source code. The demo repository includes a pre-configured Dockerfile to help you get started. Run the following commands in your terminal:
git clone https://github.com/ansuman-satapathy/demo-todoapp-for-dockerizing.git
mv demo-todoapp-for-dockerizing todoapp
cd todoapp
Step 2: Understand the Dockerfile:
A Dockerfile defines the environment and steps to build a Docker image.
The provided file uses a multi-stage build to keep the final image small and efficient.
Stage 1: Build the Application
FROM maven:3.8.4-openjdk-17-slim AS builder
WORKDIR /app COPY pom.xml
. COPY src ./src
RUN mvn clean package
Stage 2: Prepare the JAR for Execution
FROM openjdk:17-jdk-slim AS runner
WORKDIR /app COPY --from=builder
/app/target/todoapp-0.0.1-SNAPSHOT.jar app.jar
Stage 3: Create the Final Runtime Image
FROM openjdk:17-jdk-alpine AS final
WORKDIR /app COPY --from=runner /app/app.jar
. EXPOSE 8080 ENTRYPOINT [ "java", "-jar", "app.jar" ]
Explanation:
Stage 1: Uses a Maven image to compile and package the application into a JAR file.
Stage 2: Extracts the built JAR into a slim JDK image.
Stage 3: Uses a lightweight Alpine-based JDK image for runtime. The container listens on port 8080 and starts the application with java -jar.
Build the Docker Image From the project root directory, build the image. Replace your-dockerhub-username with your Docker Hub username.
docker build -t your-dockerhub-username/todoapp:latest .
docker images
Step 4: Push the Image to Docker Hub
Make the image available for deployment by pushing it to Docker Hub.
docker push your-dockerhub-username/todoapp:latest
Step 5: Launch an AWS EC2 Instance Log into the AWS Management Console and navigate to EC2.
Click Launch Instance and select an Ubuntu Server AMI.
Configure the security group to allow inbound traffic on port 8080 (Custom TCP, Source: 0.0.0.0/0).
Launch the instance and download the key pair file
Step 6: Install Docker on the EC2 Instance Connect to your instance via SSH:
ssh -i your-key.pem ubuntu@username
sudo apt update sudo apt install docker.io -y
Start and enable the Docker service:
sudo systemctl start docker
sudo systemctl enable docker
Deploy the Application on EC2 Pull the Docker image from Docker Hub:
sudo docker pull your-dockerhub-username/todoapp:latest
#Run the container, mapping port 8080 on the host to port 8080
sudo docker run -d -p 8080:8080 your-dockerhub-username/todoapp:latest
Step 8: Verify the Deployment
In a web browser, navigate to:http://:8080 .
Your To-Do application should now be running.
Troubleshooting Application not accessible?
Conclusion:
You have successfully containerized a Spring Boot application, published it to Docker Hub, and deployed it on AWS EC2. This approach provides a reproducible and scalable deployment workflow. Remember to stop or terminate your EC2 instance when not in use to avoid unnecessary charges.
