https://github.com/beh01der/docker-api-gateway-example
https://github.com/beh01der/docker-api-gateway-example
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/beh01der/docker-api-gateway-example
- Owner: Beh01der
- License: isc
- Created: 2016-03-31T00:55:41.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T10:20:43.000Z (over 3 years ago)
- Last Synced: 2025-04-30T16:35:28.276Z (about 1 year ago)
- Language: JavaScript
- Homepage: https://memz.co/api-gateway-microservices-docker-node-js/
- Size: 43.9 KB
- Stars: 25
- Watchers: 2
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# docker-api-gateway-example
This project is a simple example of API Gateway pattern for [microservices](http://microservices.io/patterns/apigateway.html) implemented in Node.js.
It's **not production-ready** service and its sole purpose is to demonstrate usage of **node-docker-monitor** and **http-proxy** npm modules.
Service reacts on Docker events and as Docker containers go up and down, it creates or removes HTTP routing rules for them.
To indicate that a container is to be handled by the gateway it must have label **api_route** defining URL prefix for that container
plus it must EXPOSE port that containerised service listens on.

To be able to monitor local Docker instance, we need to make UNIX socket `/var/run/docker.sock` available to the container.
```
docker run -d --name api-gateway -v /var/run/docker.sock:/var/run/docker.sock -p 80:8080 beh01der/docker-api-gateway-example
```
An upstream service can be started with command similar to (depending on your service implementation)
```
docker run -d --name service1 -e SERVICE_NAME=service1 -l=api_route='/service1' --expose 3000 beh01der/web-service-dockerized-example
```
When upstream service containers are being discovered we should see output like
```
$ docker logs api-gateway
Registered new api route: {"apiRoute":"/service1","upstreamUrl":"http://172.17.0.2:3000"}
Registered new api route: {"apiRoute":"/service2","upstreamUrl":"http://172.17.0.3:3000"}
```
Now, we can test it
```
$ curl 127.0.0.1/service1
Hello World!
I am service 1!
```