API Gateway, Software Development

KrakenD API Gateway – Quick Start With Docker

When we work on Microservices, we deal with many application services that work together to provide an IT service, e.g., an e-commerce solution. Sometimes we need to fuse these services easily into larger solutions with containerization like Docker and API gateway like KrakenD. This post shows how to use a Docker image of the KrakenD API gateway.

Must-have KrakenD API Gateway Configuration JSON File

Before starting up an instance of the KrakenD API gateway in any form, we need to craft a JSON file. We can also create the file using the KrakenD Designer. Then, use that file to start up the API gateway. Therefore, KrakenD has to have access to that file during start-up.

There are two ways to do that: 1) copy the configuration file when we build the Docker image, or 2) reference that file from a shared volume.

Copy Configuration File To Image During Docker Build

When we build a Docker image locally, we need a Dockerfile that specifies how we generate the image. Consider the following sample Dockerfile.

The Dockerfile uses the latest KrakenD Docker image from devopsfaith/krakend to create a new image. Then, we run the following command to build a new Docker image locally.

Note that we need the /config/krakend.json file in the current directory. Therefore, our local folder structure looks like the following.

Finally, we can start the KrakenD API gateway using the following command.

If we copied the kraken.json  file to another location in the Docker image, we would need to point to that file when starting up a container.

Copy Configuration File To a Shared Docker Volume

Alternatively, we can upload the krakend.json  file to a Docker volume, and multiple instances of the KrakenD API gateway can use it. This option does not require us to create a new Docker image. Therefore, we could still use the original image from devopsfaith/krakend. Let us say we have the following folder structure.

Next, we create a Docker volume.

Then, we copy the config folder and its content to that volume. Although there are many ways to copy data from the host to the Docker volume, we only choose one for this post – using a temporary Docker container, and we need two terminal windows for this.

On the first window, run the following command.

Then, on the second window (working directory must be the same), run the following command to copy the config folder and its content to krakend-api-gateway-config volume and under the /data directory.

We then would have the following.

KrakenD API Gateway With Docker

If multiple instances of KrakenD API gateway need to access the same configuration file in a shared Docker volume, we need to specify accordingly.  Consider the following command.

The command creates a Docker container and mounts our krakend-api-gateway-config volume as /mykrakend within the container. Then, it starts up the KrakenD API gateway using the configuration file /mykrakend/config/krakend.json.

If we had the following content for the krakend.json, we would access the endpoint via HTTP://localhost:8000/myendpoint.

We can also create a Docker volume from a local directory, but we won’t cover it here. No doubt, this article is not comprehensive. We went through basic Docker stuff that aims to show how to start the KrakenD API gateway up as a Docker container.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like