How to generate a docker image of your Spring Boot application
Tags: spring, java, aws, docker
Published: 2021-10-31 22:34:49

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mi3b6pnbocv006yb2sn.png)

<h1> What is Docker? </h1>
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.
<br>
<h1> Create account <a href="https://hub.docker.com/"> Docker Hub </a>
Do docker download on you pc: <a href="https://hub.docker.com/editions/community/docker-ce-desktop-windows"> download </a>

now, with your docker hub account, lets to create a repository!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzcoa4x12d42sm0c80hn.png)


Well, now we have a public repository
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17bf5yy6ffwo4i2rb00d.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbvbmplbot6ym0bnunf4.png)

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

<hr>
<h3> Editing build.gradle </h3>
First, we need to add the build script, to make build before up the application to image

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqahby4wbinn0zwnk0pk.png)

```
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:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grig3rwsd264fkry5045.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/saggn0y7qyqdi870gj4w.png)

```
bootJar {
    baseName = 'aws_training_01'
    version = '1.0.0'
    mainClassName = "com.pedrospiet.spring01.AwsSpring01Application"
}
```
<hr>
now, we are going to add the tasks that make our project generated the docker image

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bu45rig7bch1gezyqkvs.png)

```

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"])
}
```

<h2> It Works </h2>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zg3sul0j63kt296ovq9d.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uecpf29vtma0ntxppdi.png)

<h2> Running image in a container on Intelij IDEA </h2>
First press ALT + 8
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok8lk4y02dok2amzarxt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djaf02kni2ovnhou7hp7.png)

go to APPLY and RUN
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcu15w1u4gnu1o3c1ff5.png)
now we can test in insomnia
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7l7v8xfgllxxlftim75u.png)

<h3> IT WORKS! </h3>