DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot With Kubernetes
  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • Smart Deployment Strategies for Modern Applications

Trending

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • Why Your AI Agent's Logs Aren't Earning Trust
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Push Spring Boot Docker Images on ECR

Push Spring Boot Docker Images on ECR

This in-depth look at how to Spring Boot Docker images to the Amazon EC2 Container Registry provides a good example of Spring, Java, the cloud, and container usage.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Feb. 07, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

On a previous blog, we integrated a Spring Boot application with EC2. It is one of the rawest forms of deployment that you can have on Amazon Web Services.

On this tutorial, we will create a docker image with our application which will be stored to the Amazon EC2 container registry.

You need to have the AWS CLI tool installed.

We will get as simple as we can with our Spring application, therefore we will use an example from the official Spring source page. The only changes applied will be on the packaging and the application name.

Our application shall be named ecs-deployment

 rootProject.name = 'ecs-deployment' 

Then we build and run our application

 gradle build gradle bootRun 

Now let’s Dockerize our application.
First, we shall create a Dockerfile that will reside on src/main/docker.

 FROM frolvlad/alpine-oraclejdk8 VOLUME /tmp ADD ecs-deployment-1.0-SNAPSHOT.jar 
 app.jar 
 RUN sh -c 'touch /app.jar' 
 ENV JAVA_OPTS="" ENTRYPOINT 
 [ "sh", "-c", 
 "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] 

Then we should edit our Gradle file in order to add the Docker dependency, the Docker plugin, and an extra Gradle task in order to create our Docker image.

buildscript {
    ...
    dependencies {
        ...
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

...
apply plugin: 'docker'


task buildDocker(type: Docker, dependsOn: build) {
    push = false
    applicationName = jar.baseName
    dockerfile = file('src/main/docker/Dockerfile')
}

And we are ready to build our Docker image.

 ./gradlew build buildDocker 

You can also run your Docker application from the newly created image.

 docker run -p 8080:8080 -t com.gkatzioura.deployment/ecs-deployment:1.0-SNAPSHOT 

The first step is to create our ECR repository

 aws ecr create-repository --repository-name ecs-deployment 

Then let us proceed with our Docker registry authentication.


 aws ecr get-login 


Then run the command given in the output. The login attempt will succeed, and you're ready to proceed to push your image.

First, tag the image in order to specify the repository that we previously created, and then do a Docker push.


 docker tag {imageid} 
 {aws account id}.dkr.ecr.{aws region}.amazonaws.com/ecs-deployment:1.0-SNAPSHOT 
 docker push {aws account id}.dkr.ecr.{aws region}.amazonaws.com/ecs-deployment:1.0-SNAPSHOT 

And we are done! Our Spring Boot Docker image is deployed on the Amazon EC2 container registry.

You can find the source code on GitHub.

Docker (software) Spring Framework Spring Boot push

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot With Kubernetes
  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • Smart Deployment Strategies for Modern Applications

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook