Skip to content

Commit 4cd93a9

Browse files
committed
Virtual threads - blog post 6
1 parent adbabac commit 4cd93a9

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

_posts/2023-09-19-virtual-thread-1.adoc

+1-3
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,7 @@ Next, we will cover:
359359
- https://quarkus.io/blog/virtual-threads-3/[How to test virtual threads applications]
360360
- https://quarkus.io/blog/virtual-threads-4/[How to process Kafka messages using virtual threads]
361361
- https://quarkus.io/blog/virtual-threads-5/[How to build a native executable when using virtual threads]
362-
- How to containerize an application using virtual threads (in JVM mode) (_to be published_)
363-
- How to containerize an application using virtual threads in native mode (_to be published_)
364-
- How to process Kafka messages using virtual threads (_to be published_)
362+
- https://quarkus.io/blog/virtual-threads-6/[How to containerize an application using virtual threads]
365363
- What are we exploring to improve the virtual thread support in Quarkus (_to be published_)
366364

367365
To know more about the virtual thread support in Quarkus, check the https://quarkus.io/guides/virtual-threads[Virtual thread reference guide].
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
layout: post
3+
title: 'Containerizing virtual thread applications'
4+
date: 2023-12-14
5+
tags: virtual-threads native
6+
synopsis: 'Learn how to containerized applications using virtual threads.'
7+
author: cescoffier
8+
---
9+
:imagesdir: /assets/images/posts/virtual-threads
10+
11+
In another https://quarkus.io/blog/virtual-threads-2/[blog post], we explored how to implement a CRUD application with Quarkus to harness the power of virtual threads.
12+
This post continues from that point, explaining how to containerize the application.
13+
Containerization involves packaging the application into a container image, and we'll use the `quarkus-container-image-jib` extension for this purpose.
14+
Quarkus offers other extensions for containerization, such as `quarkus-container-image-docker`, so choose the one that suits your preference.
15+
16+
The full code for this blog post is available in the `crud-example` directory of the https://github.com/quarkusio/virtual-threads-demos[virtual-threads-demos GitHub repository].
17+
18+
== Adding Jib to the Project
19+
20+
First, add the `quarkus-container-image-jib` extension to the project:
21+
22+
[source, xml]
23+
----
24+
<dependency>
25+
<groupId>io.quarkus</groupId>
26+
<artifactId>quarkus-container-image-jib</artifactId>
27+
</dependency>
28+
----
29+
30+
Next, open the `application.properties` file and add the following properties:
31+
32+
[source, properties]
33+
----
34+
quarkus.container-image.build=true # <1>
35+
quarkus.container-image.name=virtual-threads-demos-${quarkus.application.name} # <2>
36+
----
37+
1. Enable container image build; every `mvn package` run will build a container image.
38+
2. Define the name of the container image. The `${quarkus.application.name}` placeholder is replaced by the application name, which is `crud-example` in our case.
39+
40+
== Building the Container Image for the JVM
41+
42+
To create the container image for the Java application, run the following command:
43+
44+
[source, bash]
45+
----
46+
$ mvn clean package
47+
----
48+
49+
The logs will show the container image build process, with the image being built using the `registry.access.redhat.com/ubi8/openjdk-21-runtime:1.18` base image.
50+
This image is automatically selected by Quarkus as the project uses Java 21.
51+
52+
Take note of the resulting image name: `clement/virtual-threads-demos-crud-example:1.0.0-SNAPSHOT`. The first part is your username by default, do do not forget to change it in the other commands.
53+
54+
You can run the container image with:
55+
56+
[source, bash]
57+
----
58+
$ docker run -it \
59+
-p8080:8080 \
60+
-e QUARKUS_DATASOURCE_JDBC_URL=jdbc:postgresql://docker.for.mac.localhost/rest-crud \
61+
clement/virtual-threads-demos-crud-example:1.0.0-SNAPSHOT
62+
----
63+
64+
Ensure to replace `clement` with your username.
65+
66+
NOTE: If you are running on ARM64, you may encounter a warning regarding the image's platform mismatch.
67+
You can override this using: `$ mvn clean package -DskipTests -Dquarkus.jib.platforms=linuxarm64`.
68+
69+
== Building the Container Image for the Native Executable
70+
71+
To build the container image for the native executable, use the following command:
72+
73+
[source, bash]
74+
----
75+
$ mvn package -DskipTests \
76+
-Dnative \
77+
-Dquarkus.native.container-build=true \
78+
-Dquarkus.jib.platforms=linux/arm64
79+
----
80+
81+
The `-Dnative` flag enables native compilation, and `-Dquarkus.jib.platforms=linux/arm64` specifies the target platform (required if you are on ARM64, as by default it will pick `linux/amd64`).
82+
83+
Note the property `quarkus.native.container-build=true`, which instructs Quarkus to use a container image to build the native executable, avoiding the need for GraalVM installation.
84+
85+
Run the container image with:
86+
87+
[source, bash]
88+
----
89+
$ docker run -it \
90+
-p8080:8080 \
91+
-e QUARKUS_DATASOURCE_JDBC_URL=jdbc:postgresql://docker.for.mac.localhost/rest-crud \
92+
clement/virtual-threads-demos-crud-example:1.0.0-SNAPSHOT
93+
----
94+
95+
Use the same configuration trick for the database connection, and replace `clement` with your username.
96+
97+
== Pushing the Container Image
98+
99+
Quarkus can push the container image to a registry.
100+
To push to the GitHub container repository, use these properties:
101+
102+
[source, properties]
103+
----
104+
quarkus.container-image.registry=ghcr.io
105+
quarkus.container-image.group=cescoffier
106+
quarkus.container-image.username=cescoffier
107+
quarkus.container-image.password=${GITHUB_TOKEN}
108+
----
109+
110+
The `GITHUB_TOKEN` environment variable configures the GitHub token, which must have permission to create packages. Push the container image using:
111+
112+
[source, bash]
113+
----
114+
$ mvn clean package -DskipTests -Dquarkus.container-image.push=true
115+
----
116+
117+
Append `-Dnative` for native images.
118+
119+
Multi-architecture container images can be created using:
120+
121+
[source, bash]
122+
----
123+
$ mvn clean package -DskipTests -Dquarkus.container-image.push=true -Dquarkus.jib.platforms=linux/amd64,linux/arm64
124+
----
125+
126+
This option is not applicable for native executables, howver, it is very convenient for JVM applications as you can then use the same container image on different platforms.
127+
128+
== Summary
129+
130+
This blog post demonstrated how to containerize a virtual thread application using Quarkus and the Jib container image extension. Both JVM applications and native executables were covered.
131+
132+
In the next post, we'll explore deploying this application to Kubernetes and OpenShift.

0 commit comments

Comments
 (0)