Docker: Deploying with gradle - bradsorour/notes GitHub Wiki
Docker is a great tool for both building and deploying Java applications. Building a Java application inside a container gives portable builds, you can run the build on any machine that has Docker, and build failures because of environment differences are a thing of the past. The build runs without dependencies of having the correct version of the JDK and Gradle installed.
Running a Java application inside Docker is great as well. It lets us leverage tools such as Kubernetes, and again, takes away environment dependendencies. Building inside a container, and running inside a container have different requirements however. To build a Java application, you’ll typically need a JDK, and a build tool such as Gradle. Gradle (and other build tools) provide Docker images that contain an installation of the tool. When we create an image to run the application, we prefer to strip down the image as much as possible. The smaller the image (in terms of megabytes), the better. This means a lightweight OS, a (potentially stripped down) JRE instead of a full blown JDK, and no extra tools besides the app itself.
Run the application in the IDE
Make sure this works first.
Run the application without the Docker container
./gradlew build && java -jar build/libs/spring-engine-technical-test-0.1.0.jar
Containerize the application
Create a simple "Dockerfile" in our Spring Boot project:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.enginegroup.techtest.TflApplication"]
Build a Docker image with Gradle
$ ./gradlew build docker
After the push
$ docker run -p 8081:8081 --name tflarrivals -t bradsorour/spring-engine-technical-test