Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Working with MSMQ in Windows Docker Containers

5.00/5 (1 vote)
27 Feb 2019CPOL3 min read 16.8K   626  
Quick overview and sample running a full .NET Framework 4.6.1 console app inside a Windows container, reading and writing from and to MSMQ queues.

Introduction

There are a lot of applications that use MSMQ for messaging between processes and services. Up until last year, to move these applications into Docker containers meant rewriting or at least rewriting the messaging that’s used.

Since it was announced that MSMQ will be available in Windows containers, at least this wouldn’t be required. Don’t get me wrong, I would gladly rewrite some of the applications and remove MSMQ all together, splitting applications up into more manageable micro-services, .NET Core, etc. But as with everyone, time is always a factor.

MSMQ Queues Setup

This is where I got stuck. For the service inside the Docker container to connect to the remote MSMQ, certain permissions need to be set. Ensure that the queues you want to connect to had Everyone and ANONYMOUS LOGON added with FullControl.

Test App and layout

In order to test MSMQ and how it would be used from a container, I decided to write a quick and small “dummy” console app. To fit in with most of the existing solutions, it must be full .NET Framework.

The app is very simple, with a MSMQ reader and a MSMQ writer. It will read from one queue on a remote machine somewhere, and forward the message to another queue on a remote machine.

To make things easier for myself, I just built a shortcut into the app to send messages from a local run console into the remote MSMQ queue. These messages would then be picked up by the console service inside the Docker container.

Some Code and Docker Commands

Dockerfile

The dockerfile will create the required image from the Microsoft/windowsservercore base image, version 1803. Enable MSMQ windows feature on the container. (Note that this will not succeed if the host that runs the container is not at least Windows 10 version 1803 or Windows Server 2016 version 1803. Copy the test app into the container and set the entry point.

Docker
# escape=`
FROM microsoft/windowsservercore:1803
RUN powershell -Command Enable-WindowsOptionalFeature -Online -FeatureName MSMQ-Server -All
COPY . C:\test
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; 
                       $ProgressPreference = 'SilentlyContinue';"]
WORKDIR C:\test\app
ENTRYPOINT C:\test\app\TestDockerMsmq.exe

Spinning Up the ContaIner From the Image Created by the Dockerfile

During my testing, I spun up the container as interactive. This way, I could easily check the logs on the console app as the messages are read from MSMQ.

Image 1

The queue names are collected by mounting a volume to the container. This way, the same container can be executed again for testing with different queues. A queues.json file is loaded as soon as the service starts, found on the app path under a folder "Configs". Example "C:\test\app\Configs".

This file simply contains the queue names:

JSON
{
  "ReadFromQueueName": "FORMATNAME:DIRECT=TCP:{your server ip}\\private$\\meta.in",
  "WriteToQueueName": "FORMATNAME:DIRECT=TCP:{your server ip}\\private$\\meta.out"
}

Once the container is running, you will see the console app connecting to your MSMQ queue to start reading. Any messages sent to this queue will be picked up and forwarded.

Image 2

Messages were sent from running the same app with a /writeonly argument. The queues.json file needs to be updated when running as a sender only. Set the WriteToQueueName queue name to the same name that the service in the docker container would read from...

Image 3

And whoala, nice MSMQ messaging through Docker.

Points to Keep in Mind

Overall, from the initial testing, MSMQ messaging seems to run quite smoothly from within Windows Docker containers.

As long as certain criteria are met and are in place:

  • Windows host must be at least version 1803.

    Image 4

  • The Windows Docker base image must be at least version 1803.

    Image 5

  • Security settings are set on relevant MSMQ queues:
    • Everyone and ANONYMOUS LOGON requires “Full Control” access.

    Image 6

Happy coding with Docker...

History

  • 27th February, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)