Azure IoT Edge Preview 2 – Module Networking

By default Docker prevents the outside world from communicating with the module running in the container unless you configure a host port to container port mapping.

The following container create options, used while setting the module, open a port into the container:

    "ExposedPorts": {
      "5672/tcp": {}
    "HostConfig": {
      "PortBindings": {
        "5672/tcp": [
            "HostPort": "5672"

The example above maps the host's 5672 port to the container's 5672 port.  The port numbers can be different, for example, two different modules might want to listen to port 80 so the mappings might use host ports 81 and 82.

Each container also gets its own IP address.  The "docker inspect <container id>" command shows that the container is connected to a network called "azure-iot-edge" that has IPAddress property, which is the IP address of the container.  The easiest way to access the container though is to use the container's Hostname in the Uri rather than the IP address.  The Hostname matches the container id.

Within the module's code you can listen to the port on any IP address using code like:

   IPAddress address;

  if (Socket.OSSupportsIPv4)
    address = IPAddress.Any;

  if (Socket.OSSupportsIPv6)
    address = IPAddress.IPv6Any;

In my case I use AMQP.Net Lite for AMQP TCP communication into the module.  The ContainerHost address URI, such as "amqp://guest:guest@localhost:5672", uses localhost which causes that library to listen on any address.  Other processes running on the host call it using "amqp://guest:guest@<container id>:5672".