In this blog post, we’ll discuss how to use API Builder Standalone to go from zero to a Dockerized API Builder service that can be run in any Docker-compatible infrastructure.
In this example, we’ll use MongoDB as our data source. You will need to have a MongoDB instance with a database already created with at least one Collection with data in it. I used MongoDB Atlas for my example. It’s free and hosted/managed.
We’ll leverage a few online guides from the Axway Documentation site and our data source will be MongoDB. We’ll also demonstrate how to pass in the MongoDB password at runtime as an environment variable, so that we are not hard-coding the password in our project.
Create and Test Your API Builder Project
- Create a new API Builder project. See API Builder Getting Started Guide
api-builder init lbdemoapi
NOTE: My project name is lbdemoapi
- Run your project to make sure it is working
cd ./lbdemoapi npm install --no-optional npm start
- Stop your project using Ctrl-C
- Install the MongoDB Connector
npm install @axway/api-builder-plugin-dc-mongo
- Configure MongoDB Connector by editing the connector config file, /conf/mongo.default.js as follows:
module.exports = { connectors: { mongo: { connector: '@axway/api-builder-plugin-dc-mongo', url: `mongodb://lbrenman:${process.env.MONGO_PWD}@cluster0-shard-00-00-merks.mongodb.net:27017,cluster0-shard-00-01-merks.mongodb.net:27017,cluster0-shard-00-02-merks.mongodb.net:27017/test01?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true`, generateModelsFromSchema: true, modelAutogen: true } } };
NOTE: I am using MongoDB Atlas, the MongoDB Managed Service on AWS, for my MongoDB. The DB name is test01. I will pass in my MongoDB password as a Nodejs environment variable, MONGO_PWD, when I start my API Builder project. Finally, I am generating models from schema and automatically generating APIs for the models. You will need to edit the url for your MongoDB instance, username and database name accordingly.
NOTE: Make sure you enclose your url in back ticks (`) and not single quotes (‘)
- Run and test your project and APIs
MONGO_PWD=xxxxxx npm start
NOTE replace xxxxxx with your MongoDB instance password
- You should be able to call your APIs as follows:
curl 'https://localhost:8080/api/mongo/dog'
NOTE: My MongoDB database, test01, contains a Collection called dog. Also, I edited the API Builder /conf/default.js config file and set APIKeyAuthType to ‘none’ to disable API authentication since I will be adding authentication with the Axway API Gateway. This won’t be covered in this blog post.
Dockerize Your API Builder Service
Now that we have an API Builder Project built and tested, we can proceed to Dockerize it so that we can run it in our production environment
- We’ll follow the Dockerize an API Builder Service documentation.
- In your project folder, run the following docker command in order to build the docker image:
docker build -t lbdemoapiimage ./
NOTE: My Docker image name is lbdemoapiimage. This command works since the API Builder Project contains a Docker file that can be used to create a Docker Image. This file is automatically created when the project is created.
- Check that your image was created by using the following command:
docker image ls -a
You should see a response similar to below (scroll within table to see more columns):
REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 092dd3c179b0 About an hour ago 144MB lbdemoapiimage latest 980f7ced0673 About an hour ago 144MB <none> <none> 7987dad2194c About an hour ago 68.3MB <none> <none> e260435790a7 About an hour ago 68.3MB node 8-alpine dd574b216ad7 12 days ago 68MB
- Run your container using the following:
docker run --name lbdemoapi -p 8080:8080 -e MONGO_PWD=xxxxxxx lbdemoapiimage
NOTE: I am passing in the MONGO_PWD environment variable. My container name is lbdemoapi. I am mapping the container port 8080 to the host port 8080. You will need to replace as necessary.
- Make sure your container is running using the following command:
docker image ls -a
You should see a response similar to below (scroll within table to see more columns):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 900616179dd3 lbdemoapiimage "node ." About an hour ago Up About an hour 0.0.0.0:8080->8080/tcp lbdemoapi
- You should be able to call your API’s the same as before:
curl 'https://localhost:8080/api/mongo/dog'
At this point, we are done. We have a Docker containerized Microservice that can be run in any Docker compatible infrastructure.
Conclusion
In this blog post, we saw how easy it is to create an API Builder project and Dockerize it, so that your Microservices can be run in any Docker compatible infrastructure.