What is Docker?
Docker is a set of platform products as services that uses virtualizations on Operational System level for giving us softwares calleds "containers". The containers are isolated from each other and a group yours softwares, libraries and configuration archives.
Create account Docker Hub
Do docker download on you pc: download
now, with your docker hub account, lets to create a repository!
Well, now we have a public repository
now, do login into the docker hub app, if you use linux, use the command below on terminal:
docker login -u {you_username}, tap enter and write your password.
now, you should to create an archive called "Dockerfile" in root directory, and write those commands:
FROM openjdk:15
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.spiet.aws_spring01.AwsSpring01Application"]
obs: change the com.spiet.Aws... to your package with initialization class
Editing build.gradle
First, we need to add the build script, to make build before up the application to image
buildscript {
ext {
springBootVersion = '2.5.6'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.22.2")
}
}
obs: see that i need the dependencie palantir.gradle.docker in buildScript, so, in plugin sessons below, we need to add
id 'com.palantir.docker' version '0.22.2' too.
then, below in group:
change the group to your dockerId in docker hub.
NEXT, below, we need to add a bootJar with repository informations and mainClass of application:
bootJar {
baseName = 'aws_training_01'
version = '1.0.0'
mainClassName = "com.pedrospiet.spring01.AwsSpring01Application"
}
now, we are going to add the tasks that make our project generated the docker image
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
tags "${bootJar.version}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
It Works
Running image in a container on Intelij IDEA
click on run
In image session, we can found our image, so click with right button and go to "Create Container"
go to APPLY and RUN
now we can test in insomnia
Top comments (0)