Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Dockerfile Maven plug-in

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use the Dockerfile Maven plug-in". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Dockerfile Maven plug-in use

This is a Maven plug-in that seamlessly integrates Docker with Maven, and you can easily package Docker image using Maven (note: the original project docker-maven-plugin is no longer recommended).

Design objectives:

Don't try to do anything. This plug-in uses Dockerfiles to build Docker projects and is mandatory.

Integrate the Docker build process into the Maven build process. If you bind the default phases, you will get a Docker image when you type mvn package. When you type mvn deploy, your image is push.

Let goals remember what you're doing. You can enter mvn dockerfile:build and the following mvn dockerfile:build and mvn dockerfile:push are all fine. This also eliminates previous commands like mvn dockerfile:build-DalsoPush; instead, you can just use mvn dockerfile:build dockerfile:push.

Integrate with Maven build reactor. You can rely on Docker image,Maven built by another project in one project to build the project in the correct order. This is useful when you want to run integration tests that involve multiple services.

The project complies with Open Code of Conduct. To participate in contributing code, you need to follow this code rule.

View the update log for a list of releases

Set-up

The plug-in requires Java 7 or later and Apache Maven 3 or later. To run integration tests or use the plug-in in development, you need a working Docker.

Examples

For more examples, see the integration test directory.

In particular, advanced testing shows a set of services made up of two microservices that use helios-testing for integration testing.

This will configure the plug-in to build the image using mvn package and push using mvn deploy. Of course, you can also build explicitly with mvn dockerfile:build.

Com.spotify dockerfile-maven-plugin ${dockerfile-maven-version} default build push spotify/foobar ${project.version} ${project.build.finalName} .jar

The corresponding Dockerfile may be as follows

FROM openjdk:8-jreMAINTAINER David Flemstr ö m ENTRYPOINT ["/ usr/bin/java", "- jar", "/ usr/share/myservice/myservice.jar"] # Add Maven dependencies (not shaded into the artifact; Docker-cached) ADD target/lib / usr/share/myservice/lib# Add the service itselfARG JAR_FILEADD target/$ {JAR_FILE} / usr/share/myservice/myservice.jar advantages

There are many advantages to using this plug-in for project construction.

Faster build time

This plugin allows you to make better use of Docker caching, greatly speeding up your build by letting you cache Maven dependencies in your image. It also encourages the avoidance of maven-shade-plugin, which greatly speeds up the build.

Consistent build lifecycle

You don't need to be like this anymore:

Mvn packagemvn dockerfile:buildmvn verifymvn dockerfile:pushmvn deploy

Just use the following line of command:

Mvn deploy

With basic configuration, this ensures that image is built and push at the right time.

Docker images that depend on other services

You can rely on the Docker information of another project because this plug-in attaches project metadata when building the Docker image. Simply add this information to any project:

Com.spotify foobar 1.0-SNAPSHOT docker-info

Now, you can read information about the Docker image of the project you rely on:

String imageName = getResource ("META-INF/docker/com.spotify/foobar/image-name")

This is useful for integration tests that require the latest version of the Docker image of another project.

Note that you must register Maven extension with POM (or parent POM) to support the docker-info type:

Com.spotify dockerfile-maven-extension ${version} use other Dockerfiles-dependent Docker tools

Your project is as follows:

A/ Dockerfile pom.xmlb/ Dockerfile pom.xml

You can now use Fig or docker-compose or some other system that works with Dockerfiles to use these projects. For example, a docker-compose.yml might look like this:

Service-a: build: a / ports:-'80'service-b: build: B / links:-service-a

Now, docker-compose up and docker-compose build will work as expected.

Authentication and private Docker registry support

Starting with version 1.3.0, the plugin will automatically use the configuration in the ~ / .dockercfg or ~ / .docker / config.json file when you pulling, pushing, or building images into private registries.

In addition, if the plug-in can successfully load Google's Application default credentials, the plug-in will support Google Container Registry. If defined, the plug-in also loads Google credentials from the file pointed to by the environment variable DOCKER_GOOGLE_CREDENTIALS. Because GCR authentication needs to retrieve a short-term access code for a given credential, support for this registry will be incorporated into the underlying docker-client without having to configure the docker configuration file before running the plug-in.

GCR users may need to initialize their application default credentials through gcloud. Depending on where the plug-in is running, they may want to use their Google identity by running the following command.

Gcloud auth application-default login

Or create a service account instead.

Use maven settings.xml for authentication

Starting with version 1.3.6, you can use maven's settings.xml file for authentication instead of using docker configuration. Simply add a configuration similar to the following:

Docker-repo.example.com:8080/organization/image latest true

You can also use-Ddockerfile.useMavenSettingsForAuth=true on the command line.

Then, in your maven settings file, add the configuration for the server:

Docker-repo.example.com:8080 me mypassword

The configuration is exactly the same as other servers.

Use maven pom.xml for authentication

Starting with the version of 1.3.XX, you can use the configuration of pom itself for authentication. You only need to add a configuration similar to the following (it has been tested that an error denied: requested access to the resource is denied will be reported when push to the private repository in version 1.4.0, so it is recommended to use maven settings.xml for authentication):

Com.spotify dockerfile-maven-plugin ${version} repoUserName repoPassword ${docker.image.prefix} / ${project.artifactId} target/$ {project.build.finalName} .jar

Or simpler.

Com.spotify dockerfile-maven-plugin ${version} ${docker.image.prefix} / ${project.artifactId} target/$ {project.build.finalName} .jar

Use this command line to call:

Mvn goal-Ddockerfile.username=...-Ddockerfile.password=...Maven Goals

The Goals available for this plug-in:

GoalDescriptionDefault Phasedockerfile:build builds a Docker image from Dockerfile. Packagedockerfile:tagTag Docker image. Packagedockerfile:push pushes the Docker image to repository. Deploy skips Docker Goals binding to Maven phase

You can pass options to maven to disable docker goals.

Maven OptionWhat Does that thing Do?dockerfile.skipDisables the entire dockerfile plugin; all goals become no-ops.dockerfile.build.skipDisables the build goal; it becomes a no-op.dockerfile.tag.skipDisables the tag goal; it becomes a no-op.dockerfile.push.skipDisables the push goal; it becomes a no-op.

For example, skip the entire dockerfile plug-in:

This is the end of mvn clean package-Ddockerfile.skip 's "how to use the Dockerfile Maven plug-in". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report