ActiveMQ on Windows NanoServer using Docker

Posted in software by Christopher R. Wirz on Wed Sep 12 2018



ActiveMQ provides message queueing - which has the main benefit of resilience. When a service is down, putting your message in a queue will ensure it gets there when possible. For this reason, it is a great idea to incorporate ActiveMQ in a service oriented application. Many modern SOA (which stands for Service Orientated Architecture) apps use Docker. One of the challenges of Docker on Windows is getting the container size down. Using a multi-stage build, you can make the jump down from windowsservercore to nanoserver.

Note: When distributing a Dockerfile, it is good practice (from my experience) to include third party install files locally. This will allow the image to be built on an air-gapped networks. Also, third party hosted URLs are sometimes removed to reduce their costs.

After downloading all the necessary installers (this example uses jre-8u181-windows-x64.exe and apache-activemq-5.15.6-bin.zip), place them in the base directory with the Dockerfile. The following Dockerfile is an example of how to use the robust windowsservercore image to build capabilities for the light-weight nanoserver target image.


#Dockerfile
FROM microsoft/windowsservercore as builder

WORKDIR "C:/"

COPY "jre-8u181-windows-x64.exe" jre.exe
COPY "apache-activemq-5.15.6-bin.zip" activemq.zip

# Silently install the JRE
RUN	jre.exe /s INSTALLDIR=C:\jre WEB_JAVA=0 SPONSORS=0  && \
	DEL jre.exe

# Extract ActiveMQ
RUN	powershell -Command Expand-Archive activemq.zip C:/  && \
	REN apache-activemq-5.15.6 activemq  && \
	DEL activemq.zip

FROM microsoft/nanoserver
COPY --from=builder ["C:/jre", "C:/jre"]
COPY --from=builder ["C:/activemq", "C:/activemq"]

# Set JAVA_HOME
ENV JAVA_HOME "C:\\jre"
# Add the option to prevent heap reservation issues
ENV _JAVA_OPTIONS "-Xmx128M -Xms128M"
# Add Java to the path
ENV PATH "C:\\jre\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\"

# Admin Port
EXPOSE 8616
# Listen Port
EXPOSE 61616
# STOMP is the Simple (or Streaming) Text Orientated Messaging Protocol
EXPOSE 61613

CMD "C:\\activemq\\bin\\activemq start"

Ready to try this out?

To begin with, open Powershell and go to the directory with the "Dockerfile". Then issue the following build commands to the Docker daemon:


docker pull microsoft/windowsservercore
docker pull microsoft/nanoserver
docker build -t activemq-single-image .

This will build the image. Now, you must run the image and map the exposed ports.


docker run --name activemq -it -p 8161:8616 -p 61616:61616 -p 61613:61613 activemq-single-image

You may get an error stating Error occurred during initialization of VM Could not reserve enough space for object heap. This can be overcome by fixing the _JAVA_OPTIONS.


docker run --name activemq -it -e "_JAVA_OPTIONS=-Xmx128M -Xms128M" -p 8161:8616 -p 61616:61616 -p 61613:61613 activemq-single-image

In order to connect to the message broker, you will need to find the address on which the container is running.


docker inspect -f "{{.NetworkSettings.Networks.nat.IPAddress}}" activemq

This will provide the IP address you need to supply to your clients.

Looking for a job?