Spring Boot + Docker + AWS | Spring Boot and Docker
Chapter 1 | Spring Boot and Docker
Before you start reading, you have to know that this tutorial series is designed to be the most simpliest and easiest way for begginers, our aim here is to get beginners to start and get dirty on this type of stuff, we are not considering that this will create a professional and complete structure of service or services that you can use on production but at least will help anyone out there.
This is the first part of a series where we are going from create a docker image to deploy that image into AWS, we are going also to use an Application Load Balancer, to have some security configurations, create a pipeline for CI/CD into AWS CodePipeline and be ready for the real world.
Let's start this series with the base point. We are assuming that if you are reading this is because at least you have the knowledge to create a spring boot application, that you also have some docker knowledge, I mean, not a professional in docker but at least you know what it is and why you want to use it.
What you need for this tutorial?
- You need a blank or an already created project with spring boot, you can start here.
- You need docker in your computer, you can start here.
- You will need Maven or Gradle, whatever you feel more comfortable, take a look here.
Now let's begin!
Creating the JAR file
In order to start we need to generate the JAR file to deploy our application, we are not going to dig deep into explain how Docker works or even Spring Boot, if you are reading this it is because you already know that (hopefully).
Docker needs a file named "Dockerfile", inhere you can write all commands that docker will try to run to create an image and once the image is created the container can be created and it will know what to do with all the information that you are giving into the Dockerfile.
Our Dockerfile is going to use the JAR file that is going to be generated running a spicific Maven command but before we run that command we have to ensure that you added the next line into you POM.xml file (or gradle file if that's what you are using) the tag should be added into the build tag:
<build> <plugins> ... </plugins> <finalName>the-name-you-want</finalName> </build>
This will thell to Maven to create a JAR file with the name specified into the finalName tags. Now, let's run the next command:
Of course remember that you need to be in the root folder of your Spring Boot application. Once you run the command take a look into the target/ folder, there you will find the generated JAR file for your application.
Creating the Dockerfile
Now, the simplest Dockerfile that you can create in order to run your Spring Boot application will contain the next:
FROM openjdk:16 ADD target/the-name-you-want.jar the-name-you-want.jar ENTRYPOINT ["java", "-jar","the-name-you-want.jar"]
The first line, tells to docker that needs to download the openjdk in the version 16 (if you want a different version just change the 16 for the version of your favorite JDK) image in order to run your JAR file. The second line add a path to the file that you are going to use and run. The third line tells to docker a command that is going to run, in this case, will be the java -jar and the name of the JAR file that you need to run, for this example take a look on all the lines and you can see that the name of the JAR file matches the name that you already put into the POM.xml file and you generated using Maven. Also, you need to know that you have to create the Dockerfile into the root folder of your application since that will be the point of start for docker to look for the files that it needs.
Let's create the docker image
In order to create the docker image (and assuming that you already installed docker in your machine) let's run the next command:
docker build -f Dockerfile -t the-name-of-your-image .
Replace the-name-of-your-image with the real name of the image that you want to build. When I started learning this I always used to name the image as the JAR file, this will be useful in the future when using AWS believe me. Oh! and don't forget the dot at the end (.), this tells to docker the start point to look for the files that needs to create the image. Once you run the command you can verify if the image was created listing all the images that you have in your local computer running the next command:
If you see the name of your image listed there, congrats!!!. This will be not the end of this chapter, let's run the image into a docker container, at the end of the series you are going to run the same exact docker image but using AWS ECS (Elastic Container Service) and ECR (Elastic Container Registry).
Running a Docker container
To create and run a container run the next command:
docker run - p 8080:8080 <name-of-your-image>
This command will run a container mapping the port 8080 to a port 8080 of the container, this means that the container will open the port 8080, since Spring Boot runs into that port by default and the second 8080 port will be the port of your computer that will be open to see the container running. Next you will have to specify the name of the image that you want to run.
You will see the normal Spring Boot output, you know, something like this:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.0) ...
And there you go, you now have deployed your Spring Boot application into a Docker container (into your local machine), in the next chapter we are going to push the image into AWS ECR (something like docker hub), of course, we need to setup an AWS account and some credentials using AWS CLI, so maybe the next chapter will explain first how to setup an AWS account and setup the credentials using AWS CLI. So see you tomorrow dev. <3
Also, if you want to know more about this type of topics please go and check out some tutorials in Amigoscode. We have a lot more regarding Backend and Frontend development and DevOps too!