Site iconAxway Blog

Create an API Builder Multi-Container Application Using Docker – Part 2

API Builder and Docker

In Part 1 of this blog series, we discussed how to stand up a multi-container application using Docker, API Builder, and MongoDB.

In this blog post, we’re going to add another API Builder instance to handle an increased load. We’ll also add a load balancer based on nginx as described in this excellent blog post, Scaling Out With Docker and Nginx.

We’ll do this using Docker Compose only and won’t bother with any manual setup as we did in Part 1.

Here is a block diagram of our application:

Let’s get started.

Prerequisites

You’ll need Docker Compose installed on your machine. Refer to the online docs for installation instructions.

docker-compose.yaml

Since we covered a lot of the basics in Part 1, this post is mainly concerned with creating a docker-compose.yaml file.

My docker-container YAML is shown below:

version: '3.2'
services:
  apibmongodb1:
    image: lbrenman/apibmongodb:1.0.0
    container_name: "apibmongodb1"
    environment:
      - MONGO_URL=mongodb://admin:admin@mymongodb:27017/mydata?authSource=admin
    depends_on:
      - mymongodb

  apibmongodb2:
    image: lbrenman/apibmongodb:1.0.0
    container_name: "apibmongodb2"
    environment:
      - MONGO_URL=mongodb://admin:admin@mymongodb:27017/mydata?authSource=admin
    depends_on:
      - mymongodb

  loadbalancer:
      build: ./load-balancer
      tty: true
      links:
          - apibmongodb1
          - apibmongodb2
          - mymongodb
      ports:
          - '80:8080'

  mymongodb:
    image: mongo:latest
    container_name: "mymongodb"
    ports:
      - "27017:27017"
    volumes:
      - ~/mongodata:/data/db
    command: mongod --auth

Create a file called docker-container.yaml and paste the YAML above into it and save it.

Let’s dissect the YAML as follows:

The tricky part here is the Nginx load balancer. As we see in the YAML file above, there is a subfolder called /load-balancer that contains assets related to building the load balancer service (container).

Let’s review this in the next section.

Nginx Load Balancer

In the /load-balancer subfolder, we have a Dockerfile that is used to build the loadbalancer service. This is implicitly referenced in the docker-compose.yaml above in the build configuration option. The /load-balancer subfolder also contains an Nginx configuration file, nginx.conf, which is referenced in the Dockerfile file below.

Dockerfile

The Dockerfile is shown below and is pretty simple. It basically creates an Nginx container and copies the configuration file, nginx.conf, that we’ll discuss next.

# Use the standard Nginx image from Docker Hub
FROM nginx

# Copy custom configuration file from the current directory
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 8080

# Start Nginx when the container has provisioned.
CMD ["nginx", "-g", "daemon off;"]

Create a subfolder called load-balancer and in it, create a file called Dockerfile and paste the Dockerfile contents above into it and save it.

nginx.conf

The Nginx configuration file is shown below. It basically listens to requests on port 8080 and forwards to apibmongodb1 and apibmongodb2 (in a round robin fashion).

events { worker_connections 1024; }

http {

 upstream localhost {
    server apibmongodb1:8080;
    server apibmongodb2:8080;
 }

 server {
    listen 8080;
    server_name localhost;

    location / {
       proxy_pass https://localhost;
       proxy_set_header Host $host;
    }
  }
}

In the /load-balancer folder, create a file called nginx.conf and paste the above into it and save.

Run the Application

Now that we have our docker-compose.yaml and a subfolder called /load-balancer with our Nginx Dockerfile and configuration file, we can use docker-compose to run our app as follows:

Summary

In this blog post, we used Docker Compose to easily stand up a multi-container application based on two API Builder microservices, a MongoDB database and an Nginx based load balancer.

Exit mobile version