fabric8io docker-maven-plugin

repository·master·Indexed 23 days ago

https://github.com/fabric8io/docker-maven-plugin

A Maven plugin for building Docker images and managing containers, primarily used to automate container lifecycles for integration testing. It supports building images via Dockerfiles or assemblies, optimizing builds with Spring Boot Layered JARs, and provides a JIB build mode to create images without a local Docker daemon. Key features include the docker:copy goal for extracting files from containers and support for multiple assembly layers to improve build cache efficiency.

Tokens
38.7K
Snippets
88
Records
184
Agent score
84%

What's inside docker-maven-plugin

  1. Overview of docker-maven-plugin

    master

    The docker-maven-plugin is a Maven plugin designed for building Docker images and managing containers, specifically for use in integration testing workflows. It integrates Docker lifecycle management directly into the Maven build lifecycle.

    Requirements:

    • Maven: 3.0.5 or later
    • Docker: 1.6.0 or later

    Docker API Compatibility Notes:

    • Use Docker 1.8.1+ if you need docker:watch.
    • Use Docker 1.9+ if you need custom networks or build arguments.
    • Docker 29+ (v1.44) is required for compatibility with Docker 29+ daemons.
  2. Configure watchMode options

    master

    The watchMode parameter (property docker.watchMode) determines the reaction to detected changes:

    • build: Rebuilds the image when changes are detected in the assembly.
    • run: Restarts the container if its image changes.
    • copy: Copies changed files directly into the container (requires Docker >= 1.8). The container must be running or be a data container linked to a platform container.
    • both: Combines build and run behaviors.
    • none: Disables watching for that specific image (useful for pre-built images).
  3. Link containers for networking

    master

    You can connect containers using the <links> element within the <run> configuration. This allows one container to access another using a specific alias.

    Example: <link>database:db</link> links the image with the alias database into the current container, making it accessible via the hostname db (and typically providing environment variables like DB_ for connection details).

    <run>
      <links>
        <link>database:db</link>
      </links>
    </run>
  4. Configure container wait conditions

    master

    The <wait> section allows you to block the Maven build execution until specific conditions are met after a container starts. As soon as one condition is met, the build continues.

    If you include a <time> element, it acts as a timeout for other conditions (like http or log). If the timeout is reached before the condition is met, the build aborts. If only <time> is specified without other conditions, the build simply waits for that duration and then proceeds.

    You can use Maven properties (e.g., ${host.port}) within any of these configuration elements.

    <wait>
      <http>
        <url>http://localhost:${host.port}</url>
        <method>GET</method>
        <status>200..399</status>
      </http>
      <time>10000</time>
    </wait>
  5. How registry resolution works in docker-maven-plugin

    master

    The plugin determines which Docker registry to use for push and pull operations based on the image name and configuration hierarchy.

    Image Name Semantics

    If the image name contains a registry part (everything before the first / that contains a . or :), that registry is used unconditionally and cannot be overwritten.

    Default Registry Resolution

    If the image name does not contain a registry part, the plugin follows this resolution order to find a registry:

    1. Image-specific configuration: The <registry> subelement within an <image> block.
    2. Global configuration: The <registry> element in the plugin configuration, or the system property -Ddocker.registry.
    3. Environment variable: The DOCKER_REGISTRY environment variable.
    4. Default: If none of the above are provided, docker.io is used.

    Fine-grained Control (Pull vs. Push)

    You can specify different registries for pulling (e.g., for base images during build) and pushing. This is useful when your base images come from one source but your final images are pushed to a private registry.

    To separate pull and push registries, use:

    • Configuration: <pullRegistry> and <pushRegistry> elements.
    • System Properties: docker.pull.registry and docker.push.registry.

    Note: If you provide a registry via command line (e.g., mvn -Ddocker.registry=myregistry:5000 ...), it will be used for both operations unless fine-grained settings are provided.

    <configuration>
      <registry>docker.jolokia.org:443</registry>
      <images>
        <image>
          <!-- Without an explicit registry ... -->
          <name>jolokia/jolokia-java</name>
          <!-- ... hence use this registry -->
          <registry>docker.ro14nd.de</registry>
        </image>
        <image>
          <name>postgresql</name>
          <!-- No registry in the name, hence use the globally
               configured docker.jolokia.org:443 as registry -->
        </image>
        <image>
          <!-- Explicitely specified always wins -->
          <name>docker.example.com:5000/another/server</name>
        </image>
      </images>
    </configuration>
  6. Map container ports to Maven properties

    master

    To achieve true isolation, you can map container-exposed ports to dynamic host ports. By specifying a Maven property name in the <run> section, the plugin will assign the dynamically chosen host port to that property. This allows your integration tests to discover the correct port at runtime.

    Example: <port>tomcat.port:8080</port> maps the container's port 8080 to a host port, which is then accessible via the Maven property ${tomcat.port}.

    <run>
      <ports>
        <port>tomcat.port:8080</port>
      </ports>
    </run>
  7. Wait for container readiness

    master

    The <wait> configuration in the <run> section allows you to pause the Maven build until a container is actually ready to handle requests. You can wait based on three criteria:

    1. HTTP: Check if a specific URL is reachable. Requires a <url> and an optional <time> (timeout).
    2. Log: Check if a specific text pattern appears in the container's log output. Requires a <log> pattern and an optional <time> (timeout).
    3. Time: A simple delay (though HTTP and Log are preferred for reliability).
  8. Use Ant-like Name Patterns for Matching

    master

    Goals that refer to images or containers (like stop or remove) support Ant-like path matching patterns. These patterns account for registry names, repository paths, and tags.

    Wildcards:

    • ?: Matches a single character.
    • *: Matches zero or more characters up to the next slash / or the tag separator :.
    • **: Matches zero or more characters up to the tag separator :.
    • **/: Matches zero or more characters up to a slash, ensuring the match ends with a slash.
    // Example patterns:
    **tomcat:jdk-11*
    **/megacorp/tomcat:*alpine
    megacorp/*-operator:* 
  9. Use portPropertyFile to export dynamic container properties

    master

    When using dynamic port assignment or capturing host IPs, the resolved values are only available in the Maven model after the container has started. This can cause issues for other plugins that resolve properties in earlier lifecycle phases.

    To solve this, use the portPropertyFile configuration option. This writes the resolved host IP and dynamic ports to a file. The keys in the file are the property names defined in your <ports> configuration, and the values are the actual attributes assigned by Docker.