Skip to content

lcoote/docker-maven-plugin-1

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Docker Maven Plugin

Project Goal:

  • Make it easy to build an app on a container, test it and push it to a Docker repository, even if it relies on other containers (e.g. a database)
  • Talk "Maven" rather than "Docker" (E.g. "package" rather than "build").
  • Keep it simple.

Goals

  • clean - delete all containers and images for the project
  • package - builds the containers based on YAML configuration
  • start - start the containers in order and ensures they are running
  • stop - stop all running containers for the project
  • deploy - push containers to Docker repository

Pre-requisites

Docker installed and Docker daemon running, see the docker getting started guide for e.g. on a mac follow these instructions.

Usage

The best example to look at is the one from the tests which creates a Drop-Wizard app and builds three containers: app (the dropwizard application) data and mysql, and then runs an integration test against the deployed app. Et voila a packaged image!

Typically, you build your app, run your standard unit tests and package it as usual. Then, you build a container with your app deployed onto it, and run integration tests against it. If they pass, deploy your jar into the Maven repository, and optionally, your image into a Docker repository.

To use the plugin, you need to define a docker directory in ${basedir}/src/main which will include a subdirectory for each container that you wish to deploy.

  • src/main/docker/ contains one folder per container for e.g. the mysql container would have a folder structure as follows:
    • mysql
      • Dockerfile a standard Docker file.
      • conf.yml configuration:
    • ...
      • Dockerfile a standard Docker file.
      • conf.yml configuration:
# additional data require to create the Docker image
packaging:
  # files to add to the build, usually used with ADD in the Dockerfile
  add:
    - target/example-${project.version}.jar
    - hello-world.yml
# optional list of port to expose on the host
ports:
  - 8080
# containers that this should be linked to, started before this one and stopped afterwards
links:
  - mysql
healthCheck:
  ping:
    - url: http://localhost:8080/health-check
      timeout: 60000

Trivial Example

Create a default maven project for e.g.

  mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DpackageName=com.example -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT

Add the following to the pom.xml

   <pluginRepositories>
       <pluginRepository>
           <id>sonatype-nexus-releases</id>
           <name>Sonatype Nexus Snapshots</name>
           <url>https://oss.sonatype.org/content/repositories/releases</url>
           <releases>
               <enabled>true</enabled>
           </releases>
           <snapshots>
               <enabled>false</enabled>
           </snapshots>
       </pluginRepository>
       <pluginRepository>
           <id>sonatype-nexus-snapshots</id>
           <name>Sonatype Nexus Snapshots</name>
           <url>https://oss.sonatype.org/content/repositories/snapshots</url>
           <releases>
               <enabled>false</enabled>
           </releases>
           <snapshots>
               <enabled>true</enabled>
           </snapshots>
       </pluginRepository>
   </pluginRepositories>
    ...
   <build>
       ...
       <plugins>
           ...
           <plugin>
               <groupId>com.alexecollins.docker</groupId>
               <artifactId>docker-maven-plugin</artifactId>
               <configuration>
                   <!-- your installed version -->
                   <version>1.9<version>
                   <!-- used for push -->
                   <username>alexec</username>
                   <email>[email protected]</email>
               </configuration>
           </plugin>
       </plugins>
   </build>

Create your ${basedir}/src/main/docker directory and create a subfolder for your application container

 mkdir -p src/main/docker/app

Define your Dockerfile and conf.yml and place in ${basedir}/src/main/docker/app

   src/main/docker/app
   ├── Dockerfile
   └── conf.yml

You can now invoke functionality from the plugin, information on the plugin can be found by running the following command

 mvn docker:help

For e.g. to build containers from their Dockerfile and conf.yml files, run the following command

 mvn docker:package

Tips

Tear down Docker:

docker ps -a -q | xargs docker rm
docker images -a -q | xargs docker rmi

Port forward:

VBoxManage controlvm boot2docker-vm natpf1 "8080,tcp,127.0.0.1,8080,,8080"

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published