docker-maven-plugin

A maven plugin for docker

License

License

Categories

Categories

Maven Build Tools Docker Container Virtualization Tools
GroupId

GroupId

com.spotify
ArtifactId

ArtifactId

docker-maven-plugin
Last Version

Last Version

1.2.2
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

docker-maven-plugin
A maven plugin for docker
Project URL

Project URL

https://github.com/spotify/docker-maven-plugin
Source Code Management

Source Code Management

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

Download docker-maven-plugin

How to add to project

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
</plugin>

Dependencies

compile (7)

Group / Artifact Type Version
com.spotify : docker-client jar 8.16.0
com.google.auth : google-auth-library-oauth2-http jar 0.6.0
com.typesafe : config jar 1.2.0
org.apache.maven : maven-core jar 3.3.3
org.apache.maven : maven-artifact jar 3.3.3
org.eclipse.jgit : org.eclipse.jgit jar 3.2.0.201312181205-r
com.fasterxml.jackson.core : jackson-databind jar 2.9.7

provided (3)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.2
com.google.code.findbugs : annotations jar 3.0.1
com.google.code.findbugs : jsr305 jar 3.0.1

test (8)

Group / Artifact Type Version
org.apache.maven.plugin-testing : maven-plugin-testing-harness jar 3.3.0
org.apache.maven : maven-compat jar 3.3.3
org.mockito : mockito-all jar 1.10.19
eu.codearte.catch-exception : catch-exception jar 1.4.4
junit : junit jar 4.12
org.assertj : assertj-core jar 2.3.0
org.springframework : spring-test jar 4.1.6.RELEASE
org.slf4j : slf4j-simple jar 1.7.19

Project Modules

There are no modules declared in this project.

docker-maven-plugin

Build Status Maven Central License

A Maven plugin for building and pushing Docker images.

Status: inactive

We recommend you use dockerfile-maven instead.

The future of docker-maven-plugin

This plugin was the initial Maven plugin used at Spotify for building Docker images out of Java services. It was initially created in 2014 when we first began experimenting with Docker. This plugin is capable of generating a Dockerfile for you based on configuration in the pom.xml file for things like the FROM image, resources to add with ADD/COPY, etc.

Over time at Spotify we have realized that the simplest way to build a Docker image from a Java project is to have the developer write the Dockerfile. The behavior of this plugin around generating Dockerfiles, copying your project directory to a "staging" directory to use as the Docker build context, etc., ultimately led to a lot of unnecessary confusion with our users that stemmed from introducing extra abstractions and a need for configuration on top of what Docker is providing.

This led to the creation of a second Maven plugin for building docker images, dockerfile-maven, which we think offers a simpler mental model of working with Docker from Maven, for all of the reasons outlined in dockerfile-maven's README.

Purpose

You can use this plugin to create a Docker image with artifacts built from your Maven project. For example, the build process for a Java service can output a Docker image that runs the service.

Setup

You can specify the base image, entry point, cmd, maintainer and files you want to add to your image directly in the pom, without needing a separate Dockerfile. If you need VOLUME command(or any other not supported dockerfile command), then you will need to create a Dockerfile and use the dockerDirectory element.

By default the plugin will try to connect to docker on localhost:2375. Set the DOCKER_HOST environment variable to connect elsewhere.

DOCKER_HOST=tcp://<host>:2375

Other docker-standard environment variables are honored too such as TLS and certificates.

Specify build info in the POM

This example creates a new image named example, copies the project's jar file into the image, and sets an entrypoint which runs the jar. Change VERSION GOES HERE to the latest tagged version.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>VERSION GOES HERE</version>
      <configuration>
        <imageName>example</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

Use a Dockerfile

To use a Dockerfile, you must specify the dockerDirectory element. If specified, the baseImage, maintainer, cmd and entryPoint elements will be ignored. The contents of the dockerDirectory will be copied into ${project.build.directory}/docker. Use the resources element to copy additional files, such as the service's jar file.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>VERSION GOES HERE</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>
    ...
  </plugins>
</build>

Usage

You can build an image with the above configurations by running this command.

mvn clean package docker:build

To push the image you just built to the registry, specify the pushImage flag.

mvn clean package docker:build -DpushImage

To push only specific tags of the image to the registry, specify the pushImageTag flag.

mvn clean package docker:build -DpushImageTag

In order for this to succeed, at least one imageTag must be present in the config, multiple tags can be used.

<build>
  <plugins>
    ...
    <plugin>
      <configuration>
        ...
        <imageTags>
           <imageTag>${project.version}</imageTag>
           <imageTag>latest</imageTag>
        </imageTags>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

Optionally, you can force docker to overwrite your image tags on each new build:

<build>
  <plugins>
    ...
    <plugin>
      <configuration>
        ...
        <!-- optionally overwrite tags every time image is built with docker:build -->
        <forceTags>true</forceTags>
        <imageTags>
           ...
        </imageTags>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

Tags-to-be-pushed can also be specified directly on the command line with

mvn ... docker:build -DpushImageTag -DdockerImageTags=latest,another-tag

Bind Docker commands to Maven phases

You can also bind the build, tag & push goals to the Maven phases, so the container will be built, tagged and pushed when you run just mvn deploy. If you have a multi-module project where a sub-module builds an image, you will need to do this binding so the image gets built when maven is run from the parent project.

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>VERSION GOES HERE</version>
  <executions>
    <execution>
      <id>build-image</id>
      <phase>package</phase>
      <goals>
        <goal>build</goal>
      </goals>
    </execution>
    <execution>
      <id>tag-image</id>
      <phase>package</phase>
      <goals>
        <goal>tag</goal>
      </goals>
      <configuration>
        <image>my-image:${project.version}</image>
        <newName>registry.example.com/my-image:${project.version}</newName>
      </configuration>
    </execution>
    <execution>
      <id>push-image</id>
      <phase>deploy</phase>
      <goals>
        <goal>push</goal>
      </goals>
      <configuration>
        <imageName>registry.example.com/my-image:${project.version}</imageName>
      </configuration>
    </execution>        
  </executions>
</plugin>

You can skip Docker goals bound to Maven phases with:

  • -DskipDockerBuild to skip image build
  • -DskipDockerTag to skip image tag
  • -DskipDockerPush to skip image push
  • -DskipDocker to skip any Docker goals

To remove the image named foobar run the following command:

mvn docker:removeImage -DimageName=foobar

For a complete list of configuration options run: mvn com.spotify:docker-maven-plugin:<version>:help -Ddetail=true

Using with Private Registries

To push an image to a private registry, Docker requires that the image tag being pushed is prefixed with the hostname and port of the registry. For example to push my-image to registry.example.com, the image needs to be tagged as registry.example.com/my-image.

The simplest way to do this with docker-maven-plugin is to put the registry name in the <imageName> field, for example

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <configuration>
    <imageName>registry.example.com/my-image</imageName>
    ...

Then when pushing the image with either docker:build -DpushImage or docker:push, the docker daemon will push to registry.example.com.

Alternatively, if you wish to use a short name in docker:build you can use docker:tag -DpushImage to tag the just-built image with the full registry hostname and push it. It's important to use the pushImage flag as using docker:push independently will attempt to push the original image.

For example:

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <configuration>
    <imageName>my-image</imageName>
    ...
  </configuration>
  <executions>
    <execution>
      <id>build-image</id>
      <phase>package</phase>
      <goals>
        <goal>build</goal>
      </goals>
    </execution>
    <execution>
      <id>tag-image</id>
      <phase>package</phase>
      <goals>
        <goal>tag</goal>
      </goals>
      <configuration>
        <image>my-image</image>
        <newName>registry.example.com/my-image</newName>
      </configuration>
    </execution>
  </executions>
</plugin>

Authentication

Since version 1.0.0, the docker-maven-plugin will automatically use any authentication present in the docker-cli configuration file at ~/.dockercfg or ~/.docker/config.json, without the need to configure anything (in earlier versions of the plugin this behavior had to be enabled with <useConfigFile>true</useConfigFile>, but now it is always active).

Additionally the plugin will enable support for Google Container Registry if it is able to successfully load Google's "Application Default Credentials". The plugin will also load Google credentials from the file pointed to by the environment variable DOCKER_GOOGLE_CREDENTIALS if it is defined. Since GCR authentication requires retrieving short-lived access codes for the given credentials, support for this registry is baked into the underlying docker-client rather than having to first populate the docker config file before running the plugin.

Lastly, authentication credentials can be explicitly configured in your pom.xml and in your Maven installation's settings.xml file as part of the <servers></servers> block.

<servers>
  <server>
    <id>docker-hub</id>
    <username>foo</username>
    <password>secret-password</password>
    <configuration>
      <email>[email protected]</email>
    </configuration>
  </server>
</servers>

Now use the server id in your project pom.xml.

<plugin>
  <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>VERSION GOES HERE</version>
    <configuration>
      [...]
      <serverId>docker-hub</serverId>
      <registryUrl>https://index.docker.io/v1/</registryUrl>
    </configuration>
  </plugin>
</plugins>

The plugin gives priority to any credentials in the docker-cli config file before explicitly configured credentials.

Using encrypted passwords for authentication

Credentials can be encrypted using Maven's built in encryption function. Only passwords enclosed in curly braces will be considered as encrypted.

<servers>
  <server>
    <id>docker-hub</id>
    <username>foo</username>
    <password>{gc4QPLrlgPwHZjAhPw8JPuGzaPitzuyjeBojwCz88j4=}</password>
  </server>
</servers>

Testing

Make sure Docker daemon is running and that you can do docker ps. Then run mvn clean test.

Releasing

Commits to the master branch will trigger our continuous integration agent to build the jar and release by uploading to Sonatype. If you are a project maintainer with the necessary credentials, you can also build and release locally by running the below.

mvn release:clean
mvn release:prepare
mvn release:perform

Known Issues

Exception caught: system properties: docker has type STRING rather than OBJECT

Because the plugin uses Maven properties named like docker.build.defaultProfile, if you declare any other Maven property with the name docker you will get a rather strange-looking error from Maven:

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.0.21:build (default) on project <....>: 
Exception caught: system properties: docker has type STRING rather than OBJECT

To fix this, rename the docker property in your pom.xml.

InternalServerErrorException: HTTP 500 Internal Server Error

Problem: when building the Docker image, Maven outputs an exception with a stacktrace like:

Caused by: com.spotify.docker.client.shaded.javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error

docker-maven-plugin communicates with your local Docker daemon using the HTTP Remote API and any unexpected errors that the daemon encounters will be reported as 500 Internal Server Error.

Check the Docker daemon log (typically at /var/log/docker.log or /var/log/upstart/docker.log) for more details.

Invalid repository name ... only [a-z0-9-_.] are allowed

One common cause of 500 Internal Server Error is attempting to build an image with a repository name containing uppercase characters, such as if the <imageName> in the plugin's configuration refers to ${project.version} when the Maven project version is ending in SNAPSHOT.

Consider putting the project version in an image tag (instead of repository name) with the <dockerImageTags> configuration option instead.

com.spotify

Spotify

Versions

Version
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.6
0.4.5
0.4.3
0.4.2
0.4.1
0.4.0
0.3.258
0.3.257
0.3.256
0.3.255
0.3.254
0.3.253
0.3.252
0.3.251
0.3.250
0.3.249
0.3.248
0.3.247
0.3.246
0.3.245
0.3.244
0.3.243
0.3.242
0.3.241
0.3.240
0.3.239
0.3.238
0.3.237
0.3.236
0.3.235
0.3.234
0.3.233
0.3.232
0.3.231
0.3.230
0.3.229
0.3.228
0.3.227
0.3.226
0.3.225
0.3.224
0.3.223
0.3.222
0.3.221
0.3.220
0.3.219
0.3.218
0.3.217
0.3.216
0.3.215
0.3.214
0.3.213
0.3.212
0.3.211
0.3.210
0.3.209
0.3.208
0.3.207
0.3.206
0.3.205
0.3.204
0.3.203
0.3.202
0.3.201
0.3.200
0.3.199
0.3.198
0.3.197
0.3.196
0.3.195
0.3.194
0.3.193
0.3.192
0.3.191
0.3.190
0.3.189
0.3.188
0.3.187
0.3.186
0.3.185
0.3.184
0.3.183
0.3.182
0.3.181
0.3.180
0.3.179
0.3.178
0.3.177
0.3.176
0.3.175
0.3.174
0.3.173
0.3.172
0.3.171
0.3.170
0.3.169
0.3.168
0.3.167
0.3.166
0.3.165
0.3.164
0.3.163
0.3.162
0.3.161
0.3.160
0.3.159
0.3.158
0.3.157
0.3.156
0.3.155
0.3.154
0.3.153
0.3.152
0.3.151
0.3.150
0.3.149
0.3.148
0.3.147
0.3.146
0.3.145
0.3.144
0.3.143
0.3.142
0.3.141
0.3.140
0.3.139
0.3.138
0.3.137
0.3.136
0.3.135
0.3.134
0.3.133
0.3.132
0.3.131
0.3.130
0.3.129
0.3.128
0.3.127
0.3.126
0.3.125
0.3.124
0.3.123
0.3.122
0.3.120
0.3.119
0.3.118
0.3.117
0.3.116
0.3.115
0.3.114
0.3.113
0.3.112
0.3.111
0.3.110
0.3.109
0.3.108
0.3.107
0.3.106
0.3.105
0.3.104
0.3.103
0.3.102
0.3.101
0.3.100
0.3.99
0.3.98
0.3.97
0.3.96
0.3.95
0.3.94
0.3.93
0.3.92
0.3.91
0.3.90
0.3.89
0.3.88
0.3.87
0.3.86
0.3.85
0.3.84
0.3.83
0.3.82
0.3.81
0.3.80
0.3.79
0.3.78
0.3.77
0.3.76
0.3.75
0.3.74
0.3.73
0.3.72
0.3.71
0.3.70
0.3.69
0.3.68
0.3.67
0.3.66
0.3.65
0.3.64
0.3.63
0.3.62
0.3.61
0.3.60
0.3.59
0.3.58
0.3.57
0.3.56
0.3.55
0.3.54
0.3.53
0.3.52
0.3.51
0.3.50
0.3.49
0.3.48
0.3.47
0.3.46
0.3.45
0.3.44
0.3.43
0.3.42
0.3.41
0.3.40
0.3.39
0.3.38
0.3.37
0.3.36
0.3.35
0.3.34
0.3.33
0.3.32
0.3.31
0.3.30
0.3.29
0.3.28
0.3.27
0.3.26
0.3.25
0.3.24
0.3.23
0.3.22
0.3.21
0.3.20
0.3.19
0.3.18
0.3.17
0.3.16
0.3.15
0.3.14
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.3
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
0.0.29
0.0.28
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.9