Deploying Your Java App with Docker and ECR: A Seamless Journey to the Cloud
In this article, we will create a simple Spring Boot application, configure it, and then dockerize it. After dockerization, we will deploy our application to ECR (Elastic Container Registry).
In subsequent articles, I will explain how to automate these processes.
Let’s start by creating a project. To create our Spring Boot application, we visit https://start.spring.io/. Then we add dependencies like Spring Web, Spring Data JPA, MySQL Driver, Lombok and generate our project by clicking the generate button. (Lombok is completely optional)
You can open your project with your favorite IDE. After opening the project, I created an entity named Person.
I also created a PersonRepository.
To access your private database on your own computer, you need to connect to the DB via the EC2 we created. For this, go to the folder where your private key is located and run the following command. Now, the RDS you positioned privately on AWS is running on port 3307 of your localhost. You can connect to this database with tools like DBeaver.
ssh -i project-first-kp.pem -f -N -L 3307:project-first-db.cb4vqat3y5mv.eu-central-1.rds.amazonaws.com:3306 ubuntu@ec2-3-67-186-224.eu-central-1.compute.amazonaws.com
I added the settings found below to the application.properties file, and my application is now ready to run.
I created an application-test.properties file and copied the same data. I only change the url and port in datasource.url. This way, we won’t have problems when we run our application after sending it to ECR.
Now, I am writing a simple controller and with this controller, we will add a data to our database and read it back.
I added a very simple DockerFile to our project.
Finally, our project structure became as shown below.
Now we have an application that connects to the DB and is dockerized. Next, we will create ECR on AWS and deploy our application there.
We log in to the AWS console. Then, we search for ECR (Elastic Container Registry). Here we create a new private repository.
We select the ECR we created and click on View push commands. We will deploy our application with these commands. Before doing this, there are 3 things we need to pay attention to.
- Changing the spring.datasource.url in our application.properties file with the RDS endpoint and port.
- If docker is not installed on our computer, install docker.
- Make docker operational.
- Install AWS CLI on your computer.
After these, we create our jar file with our first command.
mvn package
Now we need to connect to AWS.
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin aaa.amazonaws.com
After successfully connecting,
docker build -t project-first .
we complete the docker build process. A note here: If you have a Macbook M1 or M2, you need to add -platform=linux/amd64 to the end of our docker build command above. After doing these, you can ensure that your application is deployed on ECR by running your docker tag and docker push commands.
When you enter the repository, you can now see that our Docker image has been deployed.
Conclusion:
This article provides a step-by-step guide on developing a simple Java application using Spring Boot, then containerizing it with Docker, and finally deploying it to AWS’s Elastic Container Registry (ECR). We start by creating a Spring Boot project at https://start.spring.io/ and adding the necessary dependencies. Next, we create an Entity and a Repository, and establish an SSH connection through EC2 to connect to an RDS on AWS. We add our application’s configurations to the application.properties file and containerize it using a DockerFile. Using AWS CLI and Docker, we successfully deploy our application to AWS ECR. The article details this process, including practical commands and steps. In our next article, we will explore how to set up this application on AWS ECS (Elastic Container Service).
In our next article, we will look at how to set up this application on an ECS (Elastic Container Service).
Our previous journey : Create and Access, Establishing a Secure RDS in Your Private VPC with Bastion Hosts