You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/post/dockerize-spring-boot.md
+93-2Lines changed: 93 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -133,6 +133,97 @@ So even a single change could cause the jar to be re-built and copied again. We
133
133
134
134
Can't we leverage other people's work rather than trying to come up with most optimal `Dockerfile` ourself?
135
135
136
-
## Use Buildpack
136
+
## Use Buildpacks
137
137
138
-
TBD
138
+
Many of the problems that I mentioned previously comes from that the fact that we wrote our `Dockerfile` manually. So let's look another approach which does not need any Dockerfile at all.
139
+
140
+
[Buildpacks](https://buildpacks.io/) transform the source code into containers without Dockerfiles. There are multiple projects that implements the Cloud Native Buildpacks (CNBs) [spec](https://github.com/buildpacks/spec) and probably best two options for java application today is:
141
+
[Paketo](https://paketo.io/) and [Jib](https://github.com/GoogleContainerTools/jib).
142
+
143
+
### Paketo
144
+
145
+
We can use paketo by executing the following gradle (or maven) task:
146
+
147
+
```gradle
148
+
./gradlew bootBuildImage
149
+
```
150
+
151
+
The task will look at your files, configurations and create a docker image based on the result of the analyses. It's output is really user friendly and show all the parameters that it used for building the image.
152
+
153
+
```bash
154
+
> Task :bootBuildImage
155
+
Building image 'docker.io/library/dockerize-spring-boot:0.0.1-SNAPSHOT'
Successfully built image 'docker.io/library/dockerize-spring-boot:0.0.1-SNAPSHOT'
208
+
209
+
BUILD SUCCESSFUL in 1m 57s
210
+
```
211
+
212
+
Let's run and test our the newly created images as before:
213
+
214
+
```sh
215
+
$ docker run -it -p 8080:8080 --rm dockerize-spring-boot:0.0.1-SNAPSHOT
216
+
$ curl http://localhost:8080/ping
217
+
> Pong!
218
+
```
219
+
220
+
This already gives us a solid ground to build upon for production and uses all the best practices almost for free. For example we already could see two benefits:
221
+
222
+
- The size of the created docker image from the "manual"`Dockerfile` is **468MB**while the image created by paketo with default settings is **265MB**.
223
+
- The docker layers are cached. When only the java source code is changed, we can see the following message: *Reused 4/5 app layer(s)*. So we (Paketo) can optimize our build and rebuild only what's actually necesseary.
224
+
225
+
For more information please have a look on the Spring doc [Packaging OCI Images](https://docs.spring.io/spring-boot/gradle-plugin/packaging-oci-image.html).
0 commit comments