Skip to content

Katarzyna Kmiotek

Deploying Spring Boot app to AWS with Docker

Tutorial, Docker, AWS2 min read

During the Java module at CodeClan we were building Spring Boot apps to serve API to the front end framework, building models and controllers to be able access it on http://localhost:8080 and see json formatted data.
I wanted to make the API built this way to be available online.
Great source of knowledge in this matter was an article by Bowei Han. Step by step explains how to create a simple Spring Boot app, create an account on AWS, create the Docker image of the app and push it to ECS.
Because steps 1 and 2 of his tutorial cover creating a simple Spring Boot app (without H2 or JPA dependencies) I needed to do small changes in files prior to creating the Docker image.
I would highly encourage you to follow his guidance at step: 3 , 4 and 5 .
Dependencies used to created starter from https://start.spring.io/ : dependencies With controllers and DataLoader controllers Resources/application.properties file should look like this:

1# the application itself
2server.port = 8080
3
4# the datasource
5spring.datasource.url = jdbc:h2:mem:tmpdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
6spring.datasource.driverClassName = org.h2.Driver
7
8# jpa/hibernate
9spring.jpa.show-sql = false
10spring.jpa.hibernate.ddl-auto = update
11spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
12spring.jpa.database-platform = org.hibernate.dialect.H2Dialect
13
14logging.level.root=INFO
15logging.path = /var/log

After updating application.properties remember to run Maven lifecycle clean, install and build ;)

What next? Docker
You need to install Docker Desktop or Docker ToolBox (for older Mac).
To check if docker has been installed run in the terminal: docker version or docker info
At the beginning I was getting error

1Client: Docker Engine - Community
2 Version: 19.03.1
3 API version: 1.40
4 Go version: go1.12.5
5 Git commit: 74b1e89
6 Built: Thu Jul 25 21:18:17 2019
7 OS/Arch: darwin/amd64
8 Experimental: false
9Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

After spending 2h on stackoverflow to find a solution for this error I figured out that I just needed start Docker and the easiest path to it Applications/Docker/Docker Quickstart Terminal ...
So when everything is up and running - what is Docker ?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
To pack application into Docker image we need a Dockerfile in the root of app (new file, Dockerfile, no extension, capital letter D)

The file should contain information needed to mirror the localhost environment inside Docker and commands to assemble the build. The file for app using Java 8 should look like this:

1FROM openjdk:8-jdk-alpine
2
3RUN addgroup -S spring && adduser -S spring -G spring
4USER spring:spring
5# Specify JAR location
6ARG JAR_FILE=target/*.jar
7# Copy the JAR
8COPY ${JAR_FILE} app.jar
9VOLUME /tmp
10EXPOSE 8080
11# Set ENTRYPOINT in exec form to run the container as an executable
12ENTRYPOINT ["java","-jar","/app.jar"]

I came across Dockerfile Generator that when installed and ran in the root of the app generates Dockerfile or Docker-compose file based on framework and its dependencies. I think I will be using it in the future!

Now all that is left is to build an image from command line:
$ docker build -t whiskies
$ docker images should show list of repositories with one of them called whiskies.
More docker commands can be found here

And now you can jump back to Bowei’s tutorial and carry on linking Docker with AWS, pushing whiskies image to ECR and deploying it on ECS.
Or you can use my just deployed API to fetch data for 22 whiskies and 13 distilleries in Scotland
http://3.10.55.182:8080/whiskies
http://3.10.55.182:8080/distilleries
http://3.10.55.182:8080/whiskies?region=Highland
etc etc
edit. above links are not available anymore... final

© 2024 by Katarzyna Kmiotek. All rights reserved.
Theme by LekoArts