MuleSoft + Docker

  • September 14, 2020

Let’s learn how to create and use a MuleSoft Docker Image.

What is a container?

  • A container is a standard unit of software that packages code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.
  • A Docker container image is a lightweight, standalone executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
  • Container images become containers at runtime.

What is Docker?

  • Docker is a containerization platform that packages an application and all its dependencies (in a Docker container) to ensure an application works seamlessly in any environment.

Docker Container

Benefits of Docker:

  • Portability
  • Performance
  • Agility
  • Isolation
  • Scalability

Difference between a Docker image and a Docker container

A Docker image is a non-changeable file that contains libraries, source code, tools and other files needed to run applications.

A Docker container is an environment virtualized during runtime that allows users to isolate applications from the system underpinning it. These containers are compact, portable units in which you can start up an application quickly and easily.

Difference between a Docker image and a Docker container

In short, if an image is Java Class, then the container is its objects.

How to install Docker

Using a Unix machine:

  • Uninstall old versions
sudo yum remove docker  docker-client  docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
  • Set up the repository
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • Install the Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io
  • Start Docker
sudo systemctl start docker
  • Verify the Docker Engine is installed correctly by running the hello-world image
sudo docker run hello-world

How to create a Docker image

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands you can call on the command line to assemble an image. Using Docker Build, you can create an automated build that executes several command-line instructions in succession.

  • Create a file and name it “DockerFile
  • Copy and run the following command to create an image:
docker build --tag="mule-ee:4.3.0" .

DockerFile

  • Run the following command to see the image:
docker images

Run the docker images command

Dockerfile in-depth

ROM anapsix/alpine-java:8_jdk_nashorn

# Define environment variables.
ENV MULE_HOME=/opt/mule && \
    MULE_VERSION=4.3.0-ea2 && \
    MULE_MD5=1c77132085645169b32647193e4ec265 && \
    TZ=Asia/Kolkata && \
    MULE_USER=mule

# SSL Cert for downloading mule zip
RUN apk --no-cache update && \
    apk --no-cache upgrade && \
    apk --no-cache add ca-certificates && \
    update-ca-certificates && \
    apk --no-cache add openssl && \
    apk add --update tzdata && \
    rm -rf /var/cache/apk/*


RUN adduser -D -g "" ${MULE_USER} ${MULE_USER}

RUN mkdir /opt/mule-standalone-${MULE_VERSION} && \
    ln -s /opt/mule-standalone-${MULE_VERSION} ${MULE_HOME} && \
    chown ${MULE_USER}:${MULE_USER} -R /opt/mule*

RUN echo ${TZ} > /etc/timezone

USER ${MULE_USER}

# Checksum
RUN cd ~ && wget https://repository-master.mulesoft.org/nexus/content/repositories/releases/org/mule/distributions/mule-standalone/${MULE_VERSION}/mule-standalone-${MULE_VERSION}.tar.gz && \
    echo "${MULE_MD5}  mule-standalone-${MULE_VERSION}.tar.gz" | md5sum -c && \
    cd /opt && \
    tar xvzf ~/mule-standalone-${MULE_VERSION}.tar.gz && \
    rm ~/mule-standalone-${MULE_VERSION}.tar.gz

# Define mount points.
VOLUME ["${MULE_HOME}/logs", "${MULE_HOME}/conf", "${MULE_HOME}/apps", "${MULE_HOME}/domains"]

# Define working directory.
WORKDIR ${MULE_HOME}

CMD [ "/opt/mule/bin/mule"]

# Default http port
EXPOSE 8081
  • Line # 1 says what is the base image we need to use for Docker Image
  • Line # 4-8 defines different environment variables which are used later in DockerFile
  • Line # 11-17 are used to download SSL Cert for downloading mule zip
  • Line # 20 to add user mule
  • Line #22-24 to create temporary directory and directory where mule will be extracted as well giving permission to mule user on those directories
  • Line # 26 to update the timezone
  • Line # 31-35 to download mule standalone, verify its checksum and extract the mule tar.gz file
  • Line # 38 to define mount points which can later be mapped between container image and file system
  • Line # 41 to define the working directory
  • Line # 43 to run the mule engine so on start of docker container the mule engine will start automatically
  • Line # 46 to expose 8081 port

Dockerfile in Depth

NOTE: This example uses MuleSoft CE. To use MuleSoft EE, change line #31 to download the desired standalone software and add the following line to copy the valid license file:

COPY <Local-Dir>/muleLicenseKey.lic /opt/mule/conf/muleLicenseKey.lic

Running a Docker Container

Before starting Docker Container, create the following directories and make sure they have write access:

/opt/mule/apps
/opt/mule/logs

Run the following command:

docker run -d --name  mule-ee.4.3.0 -p "80:8081" -v /opt/mule/apps:/opt/mule/apps -v /opt/mule/logs:/opt/mule/logs mule-ee:4.3.0

Run the following command to check the status of the Docker Container status:

docker ps

check the docker container status

Run a Mule application within a Mule Docker Container

Create a Mule sample application using the following flow:

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

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="b8a39b7b-d853-403a-b8dd-c83f47c84bc9" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="mule-dockerFlow" doc:id="9bcaaf34-dfb3-4f01-8992-45c36ac80f6b" >
<http:listener doc:name="Listener" doc:id="36635102-6321-4129-893b-952db7bd2882" config-ref="HTTP_Listener_config" path="/test"/>
<ee:transform doc:name="Transform Message" doc:id="c51eb0e8-80de-4c54-8ae8-de8d6f0bce27" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output text/plain
---
"Mule Application Running in Docker"]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>

Generate the jar file and put that file in /opt/mule/apps folder.

Ping the following endpoint to see the output:

192.168.56.101:8081/test

Ping the endpoint to see the output

To learn more about Docker:

https://docker-curriculum.com

https://docs.docker.com/engine/reference/commandline/docker

— By Mohammad Mazhar Ansari