premise
- docker
- Container mirror warehouse
Here is an example of two
dockerhub Alibaba Cloud container image services that can be shared
Preface
This article mainly introduces jib, a docker build tool in the java field open sourced by Google.
At present , the start on github has 8.5k and the fork has 784, which is a very convenient java field docker construction tool.
The highlight is that Docker daemon is not required, which means that even if docker is not installed locally, docker images can be built through jib, and images that comply with OCI specifications can be built .
Three methods are officially supported:
This article uses the springboot project to describe it through the maven plugin.
Let me talk about the third type, jib code base, which can be used to build java docker services on self-developed platforms.
Configure pom.xml
Add the following standard tags to the file
<build>
<plugins>
...
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<from>
<image>registry.cn-hangzhou.aliyuncs.com/dragonwell/dragonwell8:8.1.1-GA_alpine_x86_64_8u222-b67</image>
</from>
<to>
<image>imageName</image>
</to>
</configuration>
</plugin>
...
</plugins>
</build>
The above content configures a result image name imageName
, which is the final built docker image address, including 容器仓库地址/镜像名称:版本号
for example registry.cn-beijing.aliyuncs.com/lyp/lanbox:v1.0
, if the warehouse address is not filled in, the default is dockerhub .
In addition, a basic image is configured registry.cn-hangzhou.aliyuncs.com/dragonwell/dragonwell8:8.1.1-GA_alpine_x86_64_8u222-b67
, which can be considered equivalent to the From statement in the Dockerfile.
If the base image or the target image requires an account password, just add an authentication information to the from tag or to tag, there are three ways:
- Configured in the docker configuration file
- Configured in maven's setting.xml
- Configure directly in the pom.xml file
This article uses the third type, which is to add an auth tag for authentication information under the from tag or to tag, for example:
<from>
...
<auth>
<username>kafeidou</username>
<password>kafeidou</password>
<auth>
...
</from>
It can also be easily configured through environment variables:
<from>
...
<auth>
<username>${env.REGISTRY_FROM_USERNAME}</username>
<password>${env.REGISTRY_FROM_PASSWORD}</password>
<auth>
...
</from>
Wherein ${env.}
the fixed portion, followed by the actual environment variable.
You can also use system properties:
mvn compile jib:build \
-Djib.to.image=myregistry/myimage:latest \
-Djib.to.auth.username=kafeidou \
-Djib.to.auth.password=kafeidou
Is it convenient to pass authentication information through parameters when building?
Continue to look configuration
at the labels container
configuration:
this configuration tag main target vessel-related content, such as:
- appRoot -> Place the root directory of the application, used for war package projects
- args -> additional startup parameters of the program.
- environment -> environment variables for the container
- format -> build a mirror of OCI specification
- jvmFlags -> JVM parameters
- mainClass -> program startup class
- ports -> Container open ports
...
There are other content for specific reference https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#container-object .
One point to note is that Ali's container mirroring service does not support OCI mirroring, so if you choose to use Ali's container mirroring service, remember to cancel the OCI format, which is canceled by default.
In addition, JVM parameters can be configured with dynamic content through environment variables, so there is no need to plan to write all startup parameters in the jvmFlags
label.
Collector G1 specify e.g., when the container starts docker run -it -e JAVA_TOOL_OPTIONS="-XX:+UseG1GC" -d registry.cn-beijing.aliyuncs.com/lyp/lanbox:v1.0
.
After all configuration items are completed, run the mvn command to mvn compile jib:build
start building the docker image.
If you see a message like this, you are successful:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 42.598 s
[INFO] Finished at: 2020-02-18T23:30:53+08:00
[INFO] ------------------------------------------------------------------------
A complete example can be viewed and downloaded on github https://github.com/FISHStack/hello-spring-cloud , welcome to communicate.
Originating in four coffee beans , reproduced, please declare the source.
Concern public dumplings number -> [four coffee beans] to obtain the latest content