we’ll build a NodeJS microservice and deploy it using a Docker compose on the local system or you can your infra like Azure or AWS.
Here are the tools we’re going to use:
- NodeJS version 10.0
- React App using create-react-app
- MongoDB 3.4.1
- Docker for Mac
Before you attempt this guide, you should have:
- Basic knowledge in NodeJS
- Basic knowledge in Docker (and Docker installed)
- Basic knowledge in MongoDB (and the database service running. If you don’t, I suggest you follow my previous article How deploy a MongoDB replica set with Docker.)
But first, what is a microservice?
A microservice is a single self-contained unit which, together with many others, makes up a large application. By splitting your app into small units every part of it is independently deployable and scalable, can be written by different teams and in different programming languages and can be tested individually. — Max Stoiber
A microservice architecture means that your app is made up of lots of smaller, independent applications capable of running in their own memory space and scaling independently from each other across potentially many separate machines. — Eric Elliot
The benefits of microservices
- The application starts faster, which makes developers more productive, and speeds up deployments.
- Each service can be deployed independently of other services — easier to deploy new versions of services frequently
- Easier to scale development and can also have performance advantages.
- Eliminates any long-term commitment to a technology stack. When developing a new service you can pick a new technology stack.
- Microservices are typically better organized, since each microservice has a very specific job, and is not concerned with the jobs of other components.
- Decoupled services are also easier to recompose and reconfigure to serve the purposes of different apps (for example, serving both the web clients and public API).
The drawbacks of microservices
- Developers must deal with the additional complexity of creating a distributed system.
- Deployment complexity. In production, there is also the operational complexity of deploying and managing a system comprised of many different service types.
- As you’re building a new microservice architecture, you’re likely to discover lots of cross-cutting concerns that you did not anticipate at design time.
- We now need to worry about scaling or Containers and managing then to get them up and running all the time.
Our services will be deployed on containers and nginx will be the central point for routing of services to the correct containers
Microservices in Node js with React. Contribute to tkssharma/e-CommerseHub development by creating an account on…
We are going to create containers for different services like mysql, mongo and node js containers for deploying microservices for Shopping cart application
These all are different services managing different parts of application like shopping cart Admin has Admin APIs for services, Cart-Auth APIs to manage authentication in the application. They all are talking to different services and different data source and on top of that we do have nginx routing to decide where to connect for a service like
ms-commerce.com will render react client app running on port 3003
ms-commerce.com/api/ will connect with Auth container
ms-commerce.com/cart/ will connect with Cart container
ms-commerce.com/admin. will connect with admin container
Docker-compose file will manage the deployment of all containers together
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration. To learn more about all the features of Compose see the list of features.
Compose is great for development, testing, and staging environments, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process.
- Define your app’s environment with a
Dockerfileso it can be reproduced anywhere.
- Define the services that make up your app in
docker-compose.ymlso they can be run together in an isolated environment.
- Lastly, run
docker-compose upand Compose will start and run your entire app.
docker-compose.yml looks like this:
For more information about the Compose file, see the Compose file reference.
Compose has commands for managing the whole lifecycle of your application:
- Start, stop and rebuild services
- View the status of running services
- Stream the log output of running services
- Run a one-off command on a service
This is a very basic setup where nginx is talking to Node API containers and they further getting data from MySQL and mongo containers.
Dockerfile file is almost the same for all containers
We are writing code in typescript and using TSC compiler to generate build and deploying the building code using nodemon on containers.
Write application and deploy them using Docker containers on your local.
I hope you enjoyed this article. I’m still exploring the NodeJS and the microservices world, so I’m open to accepting feedback and contributions.
If you enjoyed this article, recommend it to a friend.
Find out more
Learning how to use Bash commands with Docker CLI commands can help you work more effectively with Docker apps. Check out the Docker docs and my other posts to learn more.
- Docker docs: CLI
- Docker docs: ps #filtering
- DigitalOcean — Using Grep & Regular Expressions to Search for Text Patterns in Linux
- Top 10 Docker commands you can’t live without
- The ups and downs of docker-compose — how to run multi-container applications
- Clean out your Docker images, containers and volumes with single commands