DZone
Cloud Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > 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.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Feb. 07, 17 · Cloud Zone · Tutorial
Like (4)
Save
Tweet
7.39K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • What I Miss in Java, the Perspective of a Kotlin Developer
  • No-Code/Low-Code Use Cases in the Enterprise
  • 8 Must-Have Project Reports You Can Use Today

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo