Build, Package, and PUSH Spring Boot Docker App in ECR
Dockerize your app and store in AWS
Build, Package, and PUSH Spring Boot Docker App in ECR
Why should I use Docker
Docker is the most opted containerization technology where you pack all your dependencies and run any environment.
Create a docker file on the root folder of your application. Although it is not mandatory; however, it’s a preferable way to do it.
Refer my GitHub repo for example project with docker file.
Docker image
The docker file contains all the commands used to build your image. the file shown below has the minimum required dependencies, you can extend this file based on your requirement.
The below file is an example of a multistage build.
FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD
LABEL maintainer Code Tube
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean package
FROM openjdk:8-jre-alpine
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/docker-boot-ecs-0.0.1.jar /app/
ENTRYPOINT ["java", "-jar", "docker-boot-ecs-0.0.1.jar"]
or if you don’t want to add maven as part of docker build, you could create s simplified build image.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/*.jar docker-boot-ecs-0.0.1.jar
ENTRYPOINT ["java","-jar","/docker-boot-ecs-0.0.1.jar"]
Commands to Build
Create a Repository in ECR in our case our repository name could look like
455575310263.dkr.ecr.us-east-1.amazonaws.com/ecs-boot-repo:latest
Build your Docker image using the following command. The second command is used for tagging, but its almost optional.
docker build -t ecs-boot-repo .
docker tag ecs-boot-repo:latest 455575310263.dkr.ecr.us-east-1.amazonaws.com/ecs-boot-repo:latest
Before we push we need to login first. Download AWS CLI package. Create keys to log in.
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/
Click on your profile name and goto My security credentials
In the Access keys section, choose to create an access key. To view the new access key pair, choose Show. Copy your access key and secret key as you will not have access to the secret access key again after this dialogue box closes.
Your credentials will look something like this:
- Access key ID: AKIAIOSFODNN7EXAMPLE
- Secret access key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Download this key as a CSV file for future purpose.
Now configure your credentials in AWS CLI. Go to cmd and type
aws configure
Enter your access key id and secret access key from the CSV file we downloaded.
Once, you are logged into aws CLI, Now we should be able to login into ECR.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 455575310263.dkr.ecr.us-east-1.amazonaws.com
Now Push your image into ECR.
docker push 455575310263.dkr.ecr.us-east-1.amazonaws.com/ecs-boot-repo:latest
Log into your AWS console, navigate to ECR. You should be able to see your image in the repo created.
Size of Image
Take a look at the size of the image, its just 74.8 MB. This is because the base image used in the docker file contains only bare minimum things and nothing else. Alpine is a super slim distribution and is ideal to build lean images. If you look on DockerHub you’ll see that many popular images have an Alpine version.