Author: Mohammad Mazhar Ansari

In this blog, we will try to learn how we can create MuleSoft Docker Image and use it.

What is Container?

  • A container is a standard unit of software that packages up 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 your application and all its dependencies together in the form of a docker container to ensure that your application works seamlessly in any environment.

Benefits of Docker:

Running applications in containers brings a number of benefits:

  • Portability
  • Performance
  • Agility
  • Isolation
  • Scalability

Difference between a Docker Image and Docker Container:

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

Docker container is nothing but an environment virtualized during run-time to allow 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.

In short, if an Image is Java Class then Container is its Objects.

Install Docker:

I am using my unix machine for this blog so most of the steps are for Unix.

  • 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 Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io
  • Start Docker
sudo systemctl start docker
  • Verify that Docker Engine is installed correctly by running the hello-world image
sudo docker run hello-world

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 a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

  • Create a file and name is “DockerFile
  • Copy below connect in DockerFile
  • Run below command to create an Image
docker build --tag="mule-ee:4.3.0" .
  • Run below command to see the image
docker images

Dockerfile in Depth

Let’s try to explore 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

Note: This example uses MuleSoft CE. To use MuleSoft EE please change the line # 31 to download the desired standalone software and also add below line to copy the valid license file.

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

Run Docker Container

Before starting Docker Container create below directories and make sure they have write access.

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

To run docker container run below 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 below command to check the docker container status.

docker ps

Run Mule Application within Mule Docker Container

Create a Mule Sample Application with below 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 place the jar file on /opt/mule/apps folder.

Hit below endpoint and see the output.

192.168.56.101:8081/test

To learn more about docker please refer the below links:

https://docker-curriculum.com

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

Leave a Comment