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.
1 2 3 | FROM devopsfaith/krakend COPY /config/krakend.json /etc/krakend/krakend.json |
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.
1 | docker build -t turreta/krakend --file .\Dockerfile . |
Note that we need the /config/krakend.json file in the current directory. Therefore, our local folder structure looks like the following.
1 2 3 4 5 6 | C:\USERS\KARLDEV\DESKTOP\KRAKEND └───build-image (We are here!) │ Dockerfile │ └───config krakend.json |
Finally, we can start the KrakenD API gateway using the following command.
1 | docker run -p 8000:8000 turreta/krakend |
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.
1 | docker run -p 8000:8000 turreta/krakend run -d -c /user/somewhere/krakend/krakend.json |
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.
1 2 3 4 | C:\USERS\KARLDEV\DESKTOP\KRAKEND └───krakend-docker-volume (We are here!) └───config krakend.json |
Next, we create a Docker volume.
1 | docker volume create krakend-api-gateway-config |
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.
1 | docker run --rm -it --name alpine --mount type=volume,source=krakend-api-gateway-config,target=/data alpine |
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.
1 | docker cp . alpine:/data |
We then would have the following.
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.
1 | docker run -p 8000:8000 -v "krakend-api-gateway-config":/mykrakend devopsfaith/krakend run --config /mykrakend/config/krakend.json |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { "version": 2, "name": "Turreta KrakenD - API Gateway", "port": 8000, "endpoints": [{ "endpoint": "/myendpoint", "method": "GET", "backend": [{ "url_pattern": "/todos/1", "host": [ "https://jsonplaceholder.typicode.com" ] }] }] } |
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.