Top Related Projects
INACTIVE: A maven plugin for Docker
INACTIVE: A simple docker client for the JVM
🏗 Build container images for your Java applications.
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Quick Overview
Dockerfile-maven is a Maven plugin that helps build, tag, and push Docker images as part of the Maven build process. It integrates Docker image creation seamlessly into Maven projects, allowing developers to manage Docker images alongside their Java applications.
Pros
- Seamless integration with Maven build lifecycle
- Automatic tagging of Docker images based on project version
- Supports multi-stage Docker builds
- Allows for easy customization of Docker image creation process
Cons
- Limited to Maven projects, not suitable for non-Maven builds
- Requires Docker to be installed and running on the build machine
- May add complexity to simpler projects that don't require Docker integration
- Documentation could be more comprehensive for advanced use cases
Code Examples
- Basic plugin configuration in pom.xml:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
</configuration>
</plugin>
- Custom Dockerfile location:
<configuration>
<dockerfile>src/main/docker/Dockerfile</dockerfile>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
</configuration>
- Using build arguments:
<configuration>
<repository>spotify/foobar</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
Getting Started
- Add the plugin to your pom.xml file:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>your-docker-repo/your-image-name</repository>
<tag>${project.version}</tag>
</configuration>
</plugin>
-
Create a Dockerfile in your project root or specify a custom location in the plugin configuration.
-
Run Maven build command:
mvn clean package dockerfile:build
This will build your project, create a Docker image, and tag it with the project version.
Competitor Comparisons
INACTIVE: A maven plugin for Docker
Pros of docker-maven-plugin
- More mature and widely adopted plugin
- Offers more configuration options and flexibility
- Supports both Dockerfile-based and API-based image builds
Cons of docker-maven-plugin
- More complex configuration required
- Slower build process due to additional features
- Less focus on simplicity and ease of use
Code Comparison
dockerfile-maven:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
docker-maven-plugin:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker-maven-plugin-version}</version>
<configuration>
<imageName>example</imageName>
<dockerDirectory>docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
The code comparison shows that docker-maven-plugin requires more configuration, while dockerfile-maven is simpler and more straightforward. dockerfile-maven focuses on building and pushing images based on a Dockerfile, while docker-maven-plugin offers more granular control over the build process and resources.
INACTIVE: A simple docker client for the JVM
Pros of docker-client
- More comprehensive Docker API support, allowing for advanced Docker operations
- Provides a Java API for interacting with Docker, enabling deeper integration in Java applications
- Offers more flexibility for custom Docker workflows and automation
Cons of docker-client
- Requires more setup and configuration compared to the simpler dockerfile-maven
- Steeper learning curve due to its broader feature set
- May be overkill for projects that only need basic Docker image building
Code Comparison
dockerfile-maven:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
docker-client:
DockerClient docker = DefaultDockerClient.fromEnv().build();
ContainerConfig containerConfig = ContainerConfig.builder()
.image("busybox")
.cmd("sh", "-c", "while :; do sleep 1; done")
.build();
ContainerCreation creation = docker.createContainer(containerConfig);
docker.startContainer(creation.id());
The dockerfile-maven plugin is more straightforward for Maven projects, focusing on building and pushing Docker images. docker-client offers a programmatic approach, allowing for more complex Docker operations within Java code.
🏗 Build container images for your Java applications.
Pros of Jib
- Faster builds due to layer caching and parallel execution
- No need for a Docker daemon or Dockerfile
- Reproducible builds across different environments
Cons of Jib
- Less flexibility in customizing the container image
- Limited support for multi-stage builds
- Steeper learning curve for developers familiar with Dockerfiles
Code Comparison
dockerfile-maven example:
FROM openjdk:11-jre-slim
COPY target/myapp.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
Jib example:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<to>
<image>myregistry.com/myapp</image>
</to>
</configuration>
</plugin>
dockerfile-maven requires a Dockerfile and uses Docker commands to build the image, while Jib uses a Maven plugin configuration to define the image structure and build process. Jib abstracts away the Dockerfile and Docker daemon, making it easier to integrate into existing Maven projects but potentially limiting fine-grained control over the image creation process.
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Pros of Testcontainers
- Provides a wide range of pre-configured containers for various databases and services
- Allows for dynamic creation and management of containers during test execution
- Supports multiple programming languages and testing frameworks
Cons of Testcontainers
- Requires more setup and configuration for custom Docker images
- May have a steeper learning curve for developers new to container-based testing
- Can potentially increase test execution time due to container startup overhead
Code Comparison
Testcontainers:
@Container
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:14");
@Test
void testWithPostgres() {
// Use postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()
}
Dockerfile Maven:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
Testcontainers focuses on providing a Java API for managing containers during testing, while Dockerfile Maven is primarily used for building and pushing Docker images as part of the Maven build process. Testcontainers offers more flexibility for integration testing with various services, whereas Dockerfile Maven is better suited for containerizing applications and managing Docker image lifecycle within a Maven project.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Dockerfile Maven
Status: mature
At this point, we're not developing or accepting new features or even fixing non-critical bugs.
This Maven plugin integrates Maven with Docker.
The design goals are:
- Don't do anything fancy.
Dockerfile
s are how you build Docker projects; that's what this plugin uses. They are mandatory. - Make the Docker build process integrate with the Maven build
process. If you bind the default phases, when you type
mvn package
, you get a Docker image. When you typemvn deploy
, your image gets pushed. - Make the goals remember what you are doing. You can type
mvn dockerfile:build
and latermvn dockerfile:tag
and latermvn dockerfile:push
without problems. This also eliminates the need for something likemvn dockerfile:build -DalsoPush
; instead you can just saymvn dockerfile:build dockerfile:push
. - Integrate with the Maven build reactor. You can depend on the Docker image of one project in another project, and Maven will build the projects in the correct order. This is useful when you want to run integration tests involving multiple services.
This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.
See the changelog for a list of releases
Set-up
This plugin requires Java 7 or later and Apache Maven 3 or later (dockerfile-maven-plugin <=1.4.6 needs Maven >= 3, and for other cases, Maven >= 3.5.2). To run the integration tests or to use the plugin in practice, a working Docker set-up is needed.
Example
For more examples, see the integration test directory.
In particular, the advanced test showcases a
full service consisting of two micro-services that are integration
tested using helios-testing
.
This configures the actual plugin to build your image with mvn package
and push it with mvn deploy
. Of course you can also say
mvn dockerfile:build
explicitly.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
A corresponding Dockerfile
could look like:
FROM openjdk:8-jre
MAINTAINER David Flemström <dflemstr@spotify.com>
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 itself
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/myservice/myservice.jar
Important note
The most Maven-ish way to reference the build artifact would probably
be to use the project.build.directory
variable for referencing the
'target'-directory. However, this results in an absolute path, which
is not supported by the ADD command in the Dockerfile. Any such source
must be inside the context of the Docker build and therefor must be
referenced by a relative path. See https://github.com/spotify/dockerfile-maven/issues/101
Do not use ${project.build.directory}
as a way to reference your
build directory.
What does it give me?
There are many advantages to using this plugin for your builds.
Faster build times
This plugin lets you leverage Docker cache more consistently, vastly
speeding up your builds by letting you cache Maven dependencies in
your image. It also encourages avoiding the maven-shade-plugin
,
which also greatly speeds up builds.
Consistent build lifecycle
You no longer have to say something like:
mvn package
mvn dockerfile:build
mvn verify
mvn dockerfile:push
mvn deploy
Instead, it is simply enough to say:
mvn deploy
With the basic configuration, this will make sure that the image is built and pushed at the correct times.
Depend on Docker images of other services
You can depend on the Docker information of another project, because this plugin attaches project metadata when it builds Docker images. Simply add this information to any project:
<dependency>
<groupId>com.spotify</groupId>
<artifactId>foobar</artifactId>
<version>1.0-SNAPSHOT</version>
<type>docker-info</type>
</dependency>
Now, you can read information about the Docker image of the project that you depended on:
String imageName = getResource("META-INF/docker/com.spotify/foobar/image-name");
This is great for an integration test where you want the latest version of another project's Docker image.
Note that you have to register a Maven extension in your POM (or a
parent POM) in order for the docker-info
type to be supported:
<build>
<extensions>
<extension>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-extension</artifactId>
<version>${version}</version>
</extension>
</extensions>
</build>
Use other Docker tools that rely on Dockerfiles
Your project(s) look like so:
a/
Dockerfile
pom.xml
b/
Dockerfile
pom.xml
You can now use these projects with Fig or docker-compose or some
other system that works with Dockerfiles. For example, a
docker-compose.yml
might look like:
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.
Usage
See usage docs.
Authentication
See authentication docs.
Releasing
To cut the Maven release:
mvn clean [-B -Dinvoker.skip -DskipTests -Darguments='-Dinvoker.skip -DskipTests'] \
-Dgpg.keyname=<key ID used for signing artifacts> \
release:clean release:prepare release:perform
We use gren
to create Releases in Github:
gren release
Top Related Projects
INACTIVE: A maven plugin for Docker
INACTIVE: A simple docker client for the JVM
🏗 Build container images for your Java applications.
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot