Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Push Spring Boot Docker Images on ECR

DZone's Guide to

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.

· Cloud Zone
Free Resource

Deploy and scale data-rich applications in minutes and with ease. Mesosphere DC/OS includes everything you need to elastically run containerized apps and data services in production.

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.

Discover new technologies simplifying running containers and data services in production with this free eBook by O'Reilly. Courtesy of Mesosphere.

Topics:
cloud ,docker images ,spring boot ,ecr

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}