How to use node-docker-monitor module to monitor Docker containers and implement simple API Gateway for Dockerized microserivces. This article describes the most straight forward approach. Another approach (using SkyDNS and Nginx as a reverse proxy) may offer better performance and scalability.

API Gateway and microservices

Microservice – is a software architecture pattern when a single application is developed as a number of small isolated services running in separate processes and communicating with each other using some lightweight protocol (like HTTP and JSON).

This approach has great benefits like:

  • easy deployments – no need to redeploy the whole application when only one component changed
  • easy horizontal scaling – when some components receive significantly more load then the others, it’s easy to create more instances of them to distribute the load
  • easy code maintenance – codebase of a single microservice is small, and so is easier to manage
  • easy technology mixing – components may be developed using different platforms and languages: java, Node.js, Python, Golang, … which also means easy migration from one platform to another

as well as many others.

However, it has many challenges too. One of them is ability to handle and route client requests in centralised manner:

  • have singe HTTP access point where each microservice is mapped to its own RESTful path
  • have single configuration point to enforce access rules, inject authorisation tokens
  • expose only part of available microservices / endpoints, keeping the rest for internal use
  • transform HTTP requests / responses coming from / to external clients
  • dynamically change routing rules to handle services being deployed / shut down / restarted

To tackle these challenges, API Gateway pattern is often used. Essentially, API Gateway is a dynamically configured reverse proxy server. It’s usually the only component available to external users (exposed to outside world).

Implementing API Gateway for Docker containers

We will demonstrate how simple API Gateway can be implemented with Node.js using node-docker-monitor and http-proxy npm modules. This example service will subscribe to Docker events and automatically create and manage routing rules for docker containers.

First, we will register docker monitor instance and implement onContainerUp  and onContainerDown  handlers.

When a container goes up, we will check that it has api_route label defined, and if it does, we will fetch all container details – similar to what docker inspect ...  command does.

From obtained details we will generate upstream url for that container and add a new route to the collection

When container goes down, we will remove corresponding route from the collection.

Then, we will implement proxying functionality. When HTTP request is received, we will attempt to find appropriate route and if route exists, we will update request URL (url rewrite) and pass it to the destination container. If route doesn’t exist, we will return code 502 “Bad gateway” to the client.

For your convenience, I put complete source code in GitHub repository, additionally I published a docker image in DockerHub public repository.

Testing API Gateway with simple microservices

Now, let’s see it in action. We will use docker images from DockerHub: docker-api-gateway-example – which is just dockerized version of our API Gateway and web-service-dockerized-example – a simple example of dockerized web service that we built in one of our previous posts.

To be able to monitor local Docker daemon, we need to make UNIX socket /var/run/docker.sock available to the container

then we start first microservice container

and second (you may find source code of the service on GitHub).

Please note that service port has to be explicitly exposed either with docker run command or defined in Dockerfile. Otherwise, API Gateway won’t be able to determine what port service is listening on.

API Gateway routing requests to microservices

API Gateway routing requests to microservices

When upstream service containers are being discovered we should be able to see logs like


and test it with curl

We would love to hear back from you. Was this information useful?

Please, leave your comments!

Share This

Share This

Share this post with your friends!