Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use Docker Connector to Build an Image and Run Containers From Mule Flow

DZone's Guide to

How to Use Docker Connector to Build an Image and Run Containers From Mule Flow

Take a look at this article that will show you how to build a complete Docker image and container securely using Docker Connector and Mule.

· Cloud Zone ·
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Docker Connector is a secure way of connecting to the Docker engine and accessing the Docker registry from a Mule application. It executes Docker operations by calling exposed API calls as per configuration.

In this blog, I’d like to walk you through a simple demo application to show how to use the Docker Connector to build a Docker image from Dockerfile and run containers from it. Before we start, please make sure you have the Docker Connector v1.0.0 downloaded from the MuleSoft Exchange.

Prerequisites:

I am using Ubuntu 16.04 as my Docker server machine.

  • Install Docker on your server or host
  • Start Docker on TCP.
    • Stop Docker daemon if running:
      $service docker stop
    • Start Docker daemon on HTTP port 2375:
      $dockerd -H=0.0.0.0:2375
  • Create runapp.sh file in src/main/resources/docker of your Mule Flow:
    #!/bin/bash
    
    while :; do echo "Now `date`"; sleep 5; done
  • Create Dockerfile in src/main/resources/docker of your Mule Flow:
    FROM ubuntu:latest
    
    # copy the  application to the container:
    COPY runapp.sh  /
    
    # Define working directory:
    WORKDIR /
    
    CMD  ["/bin/sh","runapp.sh"]

Designing the Mule Flow With Anypoint Studio:

Steps:

  1. Before configuring Docker connector, let's create “Property Placeholder” first. Go to GlobalElements -> Component configurations -> Property Placeholder, enter the location of the file as  mule-app.properties.

  2. In the Docker Configuration, you can specify the configuration of your Docker server. You can directly add your Docker host information in the configuration, but recommend way is using the properties file. Add your configuration information in mule-app.properties file, like:

    # HTTP Docker configuration
    docker.host=
    docker.port=2375
    docker.apiVersion=1.32
    
    # Build Image from docker file parameters
    build.dockerFile=src/main/resources/docker/Dockerfile
    build.imageName=localhost:5000/echoapp
    build.imageTag=1.0
    
    # Run container and application parameters
    app.containerName=docker-echo-app-container
    In this configuration file, update docker.host with your Docker host IP address or FQDN.
  3. To configure the Docker Connector, Go to Global Elements -> Connector Configuration -> Docker: HTTP Docker Config, fill values like below:Image title

  4. After you complete the global Docker Connector configuration, click on "OK".

  5. Drag and drop HTTP connector, create default HTTP listener configuration and set pat to /runapp.

  6. Drag and drop "Docker connector" in front of HTTP connector in Mule Flow and set the Operation to "Build an image from Docker file" to build custom Docker image.

    1. Set Docker file path to ${build.dockerFile} 

    2. Set Image tags to ${build.imageName}:${build.imageTag} by creating list object manually.
    3. Check Remove containers option from build args
  7. Drag and drop "Docker Connector" again and select operation "Run Container", configure as below:
    Image titleHere, Docker connector will create a container from ${build.imageName} Docker image and run that container.

  8.  Use "Docker Connector" again to inspect created container. Select operation "Inspect Container" and set Container Name to ${app.containerName}.

  9. Finally, drag and drop "Object to JSON transformer" to return JSON response.

  10. After adding Logger in Mule flow, your flow will be like:Image title

Testing the Application

We can use any Browser or Postman and trigger Mule application by hitting localhost:8081/runapp. You will get the response like this:

Image title

Now you can verify whether the specified Docker Image has been build and Container is running on Docker host.

Image title

Complete Mule Application Flow:


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:context="http://www.springframework.org/schema/context" xmlns:docker="http://www.mulesoft.org/schema/mule/docker" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/docker http://www.mulesoft.org/schema/mule/docker/current/mule-docker.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd">
    <context:property-placeholder location="mule-app.properties"/>

    <docker:HTTP-Docker-Config name="Docker__HTTP_Docker_Config" dockerHostIP="${docker.host}" dockerHostPort="${docker.port}" apiVersion="${docker.apiVersion}" doc:name="Docker: HTTP Docker Config"/>
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="docker-build-image-and-run-application">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/runapp" doc:name="HTTP"/>
        <docker:build-image config-ref="Docker__HTTP_Docker_Config" dockerFilePath="${build.dockerFile}"  doc:name="Build image from Dockerfile" forcerm="false" pullImage="false" removeContainers="true">
            <docker:image-tags>
                <docker:image-tag>${build.imageName}:${build.imageTag}</docker:image-tag>
            </docker:image-tags>
        </docker:build-image>
        <logger message="Build Image response:  #[payload]" level="INFO" doc:name="Log build image response"/>
        <docker:run-container config-ref="Docker__HTTP_Docker_Config" imageName="${build.imageName}" imageTag="${build.imageTag}" containerName="${app.containerName}" doc:name="Run container">
        </docker:run-container>
        <docker:inspect-container config-ref="Docker__HTTP_Docker_Config" containerName="${app.containerName}" doc:name="Inspect container"/>
        <json:object-to-json-transformer doc:name="InspectContainerResponse to JSON"/>
        <logger message="Inspect container response : #[payload]" level="INFO" doc:name="Log InspectContainerResponse"/>
    </flow>
</mule>


I hope this article helps you to understand how to build docker image and run container from Mule Application using Docker Connector. Please share your review and comments on Docker Connector if any!!

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
mulesoft ,docker ,mule flow ,mulesoft | integrate - blogs - perficient ,mule studio ,docker application ,dockerization

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}