Skip to main content

Build and deploy Docker Image with Docker Maven plugin

Build a Docker Image and Run a Docker Container with docker maven plugin fabric8.io

 

Introduction

 The maven plugin for docker fabric8io/docker-maven-plugin helps us in building docker images and running containers. In case you are thinking about the difference between an image and container then please note that a running image is called container.

In this tutorial, we are going to

  • Build a docker image
  • Link it to running DB container (Mongo)
  • Start the container
  • Stop the container
Please note that Docker and Jenkins were both running on Centos (Linux) as separate processes
  1. For below steps, I have used Spring Boot Rest service project which is connected to MongoDB. See below link for reference.
  2. Using io.fabric8 plugin, under the plugin section add the io.fabric8 plugin
  3. We need to first build the image from Dockerfile, this plugin supports all the command which are in Dockerfile but I think making docker image from Docker file is much easier and simpler. So I telling the maven plugin in below step to look for Dockerfile in root( project base directory)
  4. Next step is to specify what docker container needs to do when it runs.
    1. Since we want to make sure that mongo DB container should be running before this image starts running. We add a tag called <dependsOn>.
    2. If the MongoDB is started we would like to link this container to Mongo so both can talk to each other, we specify that by adding a tag called <link>
    3. Next, we will specify on which port we would like to run this container and which port it should map to localhost machine
    4. We enable all the logging using <log> tag, so we could see any errors or debug if some problem arises.
  5. Next, we will add execution phases, we will cover these steps
    1. Remove an existing image, if it exists
    2. Build the Image
    3. Deploy the image
  6. Now run this pom.xml using mvn clean install and check your docker containers so see if it running.

Here is the complete build section from the plugin, hope this help.

If you get an error : io.fabric8:docker-maven-plugin:0.20.0:build failed: A tar file cannot include itself

Related Posts

4 thoughts on “Build and deploy Docker Image with Docker Maven plugin

  1. Hi,
    Great posts! Thank you.
    I cannot run it because I get dockerHost errors. Please help.

    When building this on Windows 10 Home, I get an error: No given, no DOCKER_HOST environment variable, no read/writable ‘/var/run/docker.sock’ or ‘//./pipe/docker_engine’ and no external provider like Docker machine configured.
    I tried different dockerHosts, like unix:///var/run/docker.sock or localhost, but then I get errors like Execution default-cli of goal io.fabric8:docker-maven-plugin:0.21.0:build failed: An API incompatibility was encountered while executing io.
    fabric8:docker-maven-plugin:0.21.0:build: java.lang.UnsatisfiedLinkError: unknown
    [ERROR] —————————————————–
    [ERROR] realm = plugin>io.fabric8:docker-maven-plugin:0.21.0
    [ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
    Etc.
    I really hope you can help.
    Regards, J

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami