Express.js Docker example
Example to show how to build a microservice with Node.js and Express.js with instructions for: Kubernetes with YAML, OpenFaaS, Docker and running locally with
Clone the repository
git clone https://github.com/alexellis/expressjs-docker \ && cd expressjs-docker
/- serves a HTML page
/links- serves a JSON response of links
/health- serves a health endpoint giving 200 OK
Try it with Kubernetes
You can first try running the example with Kubernetes, then try it with OpenFaaS to compare the experience. OpenFaaS also adds optional templates and an API to remove boilerplate coding: "look ma', (almost) no YAML!"
kubectl apply -f ./yaml kubectl port-forward deploy/expressjs 8080:8080 & curl 127.0.0.1:8080
Conservative resource limits / requests values have been set in the YAML files:
resources: limits: cpu: 10m memory: 128Mi requests: cpu: 10m memory: 128Mi
kubectl delete -f ./yaml
See also: /yaml - (optional) - YAML files to deploy to Kubernetes directly
Watch my latest video on OpenFaaS with the PLONK Stack, which is made up of Prometheus, Linkerd (optional), OpenFaaS, NATS, and Kubernetes.
If you don't already have OpenFaaS, then:
Access using the gateway's URL found via
faas-cli describe service
image: alexellis2 and replace with your own Docker Hub username in
stack.yml, then run:
See also: stack.yml - (optional) - OpenFaaS deployment file
With OpenFaaS Templates you don't need to bother with managing Dockerfiles and TCP-port bindings, unless you like that sort of thing, then you can do that too just like we did in this example.
- Microservice for Node.js with express visible
- Function for Node.js with express hidden
- Legacy Node template without express
Try one of the templates above:
faas-cli template store list faas-cli template store pull node10-express faas-cli new --lang node10-express express-fn
express-fn.yml, before then running:
faas-cli up -f express-fn.yml
What is different?
- No Kubernetes YAML files to manage
- No Dockerfile to worry about
- No index.js, no port-bindings, no Prometheus metrics to add, and no auto-scaling rules. OpenFaaS automates all of this and more.
Run it with docker
docker run --name expressjs -p 8081:8080 -ti alexellis2/service:0.3.2
Access via http://localhost:8081
docker rm -f expressjs
Run it without Docker, locally
npm install http_port=3000 node index.js
Access via http://localhost:3000
Clean up by hitting Control + C.
index.js - the entry-point for Express.js
stack.yml - (optional) - OpenFaaS deployment file
/yaml - (optional) - YAML files to deploy to Kubernetes directly
Contributing via GitHub
Both Issues and PRs have their own templates. Please fill out the whole template.
All commits must be signed-off as part of the Developer Certificate of Origin (DCO)