Build and Deploy docker images
28th of June, 2020Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
Build Image
For building image i’m using the gradle+java based code, which is available here
There are other ways that i’ve tried to use to build, other than the IDE plugin based ones, like below:
- Dockerfile: You can either
- Build the jar file through gradle and write the Dockerfile to copy the jars and run.
- Build the image using Dockerfile
- that’ll run gradle, generate jars, copy them, and run them
- or run gradle within the dockerfile to run the app
Gradle -> Jar -> Dockerfile
-
Create a task in gradle to copy all the runtime dependencies
def dependencyPath = new File("$buildDir/libs/dependency") task copyRuntimeLibs(type: Copy) { if(!dependencyPath.isDirectory()){ dependencyPath.mkdirs(); } from configurations.runtimeClasspath into dependencyPath }
-
Make jar task depend on the copyRuntimeLibs task
jar { dependsOn 'copyRuntimeLibs' }
-
In the Dockerfile
FROM openjdk:11-jdk ARG JAR_FILE=build/libs/*.jar ARG DEPENDENCY=build/libs/dependency COPY ${JAR_FILE} /app/main.jar COPY ${DEPENDENCY} /app/libs ENTRYPOINT ["java","-cp","/app/*:/app/libs/*","org.sengarkuldeep.Main"]