DEV Community

Luis Rodrigues
Luis Rodrigues

Posted on

5 1

Creating a local development environment to Kong API with Docker

Alt Text
In this article let’s create a local development environment for Kong API. This environment is very useful to test our integration between our applications and Kong API Gateway.

If you want to know more about Kong API Management, check its documentation. Beyond API Gateway, Kong has other nice features to management our APIs.

We are going to use Docker to create our local development environment. Then, please install Docker tools in your local machine if you don’t have them yet.

Three docker containers are required to create a local development environment:

  • pantsel/konga: open source admin tool for Kong API Management.
  • kong: Kong API Management
  • postgres: Database for Kong API Management

Running Kong API Gateway

First, let’s create a docker network to each initiate docker container can communicate between themselves:

docker network create kong-net

Start Postgres database to create a database for Kong API:

docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6

In this example we are using Postgres to creating a new database, however, Kong API Gateway supports Cassandra as its database as well.

Now, we need to run a data migration process to prepare Kong API database. Run the command below to start the data migration process:

docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations bootstrap

Finally, start Kong API Gateway with the command below:

docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
kong:latest

When the container is ready, two ports will be available for use in our machine (localhost):

Check Admin API documentation to know more about how to manage your APIs in Kong API Gateway.

Running Admin UI

Now that we have our Kong API Gateway ready, we can manage services, routes, and plugins for our APIs.

Those Kong API objects are managed with Admin API. For instance, the command below creates a new service to fictional “Product API” from the fictional address https://product-api.net”:

curl --location --request POST 'http://localhost:8001/services' \
--form 'name="product-api"' \
--form 'host="product-api.net"' \
--form 'protocol="https"' \
--form 'port="443"'

Sometimes, this task can be very challenging. Then, for testing purposes, we can manage our APIs in Kong API Gateway with Konga:

docker run -d -p 1337:1337 --network=kong-net --name konga -v /var/data/kongadata:/app/kongadata -e "NODE_ENV=development" pantsel/konga

Konga is an open source project that allows us to manage Kong API Gateway with a UI. You can know more about the project here.

Run the command below to start the Konga container:

Konga will be started on port 1337 from your localhost. In the first access, the application will request to create a new user.

After creating the user, we need to create a new connection between Konga and Kong API Admin to manage our services:

Alt Text

Provide a name for the new connection, the IP address where Kong Admin API is running, and click on the “Create Connection” button. Then, you can see the Konga Dashboard.

Alt Text

If you had some issue with Kong Admin URL, try to provide the IP address from the Kong docker container. Execute the command below to get the IP address from the container:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <<kong container id>>

It’s done! Now you can explore more Kong API Gateway in your local machine. Have happy findings with Kong API Gateway! :D

I hope that this can be helpful for you!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay