DEV Community

Pedro
Pedro

Posted on • Updated on

How to generate a docker image of your Spring Boot application

Image description

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!

Image description

Well, now we have a public repository
Image description

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.

Image description

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"]
Enter fullscreen mode Exit fullscreen mode

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

Image description

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")
    }
}
Enter fullscreen mode Exit fullscreen mode

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:
Image description
change the group to your dockerId in docker hub.

NEXT, below, we need to add a bootJar with repository informations and mainClass of application:
Image description

bootJar {
    baseName = 'aws_training_01'
    version = '1.0.0'
    mainClassName = "com.pedrospiet.spring01.AwsSpring01Application"
}
Enter fullscreen mode Exit fullscreen mode


now, we are going to add the tasks that make our project generated the docker image

Image description


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"])
}
Enter fullscreen mode Exit fullscreen mode

It Works

Image description

Image description

Running image in a container on Intelij IDEA

First press ALT + 8
Image description

click on run

In image session, we can found our image, so click with right button and go to "Create Container"

configure the ports:
Image description

go to APPLY and RUN
Image description
now we can test in insomnia
Image description

IT WORKS!

Top comments (0)