KONG-34 POC - Building Kong from source
https://folio-org.atlassian.net/browse/KONG-34
- 1 Building Kong Gateway from Source on Ubuntu 24.04
- 2 Running and testing Kong Gateway docker image
- 3 References
Note: Kong 3.9.1 is available as a Docker Hardened Image.
Building Kong Gateway from Source on Ubuntu 24.04
1. Prerequisites
Before you begin, ensure your system meets the following requirements:
Operating System: Ubuntu 24.04 (or a compatible Debian-based distribution).
User Privileges: A user account with
sudoaccess.Docker: Required to build a custom Docker image.
2. Clone the Kong Gateway Repository
Start by cloning the Kong Gateway source code from its official GitHub repository.
git clone https://github.com/Kong/kong.git
cd kong 3. Install System Dependencies
Kong and its build tools (Bazel/Bazelisk) require several system packages, including compilers, development libraries, and utility tools. Install them using apt.
sudo apt update
sudo apt install -y \
make \
build-essential \
g++ \
unzip \
zip \
curl \
default-jdk \
automake \
file \
git \
libyaml-dev \
libprotobuf-dev \
m4 \
perl \
pkg-config \
procps \
valgrind \
zlib1g-dev
4. Install Bazelisk
Bazelisk is the recommended way to manage Bazel versions. It automatically downloads the correct Bazel version required by your Kong project.
# Download the latest Bazelisk release for Linux AMD64
sudo curl -L https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64 -o /usr/local/bin/bazel
# Make Bazelisk executable
sudo chmod +x /usr/local/bin/bazel
# Verify the installation and trigger Bazelisk to download the appropriate Bazel version
bazel version
5. Install GitHub CLI (gh)
The GitHub CLI (gh) is a powerful tool for interacting with GitHub directly from your terminal. While not strictly required for building Kong, it's often useful in a development workflow.
# Install wget if not present
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y))
# Add GitHub CLI GPG key
sudo mkdir -p -m 755 /etc/apt/keyrings
out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg
cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
# Add GitHub CLI repository to apt sources
sudo mkdir -p -m 755 /etc/apt/sources.list.d
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
# Update apt cache and install gh
sudo apt update
sudo apt install gh -y
# Authenticate with GitHub (interactive step)
gh auth login
Note: The gh auth login command will prompt you to authenticate your GitHub account through your web browser or by pasting a token.
6. Build Kong Gateway
With all dependencies and tools in place, you can now proceed to build Kong. Ensure you are in the kong repository directory (cd kong).
6.1 Build Kong's Internal Dependencies
This command builds the internal dependencies required by Kong, including its Luarocks modules and C components.
bazel build //build:kong --sandbox_debug --verbose_failures
6.2 Modify .bazelrc file
Update the .bazelrc file in Kong's route folder to include the following release settings:
build:release --//:debug=true
build:release --//:licensing=false
build:release --action_env=BUILD_NAME=kong-eureka6.3 Perform an Official Release Build
This command compiles the final Kong Gateway binary optimized for release. The compiled artifact will be located in the bazel-bin/build/ directory.
bazel build //build:kong --verbose_failures --config release
6.4 Build a Debian Package
This step is crucial for building the Docker image as it relies on the generated .deb package. This command builds the Debian package (.deb) which will contain the compiled Kong Gateway and its dependencies. The .deb package will be located in the bazel-bin/pkg/ directory.
bazel build --verbose_failures --config release :kong_deb
After this command, you should find a file named kong.amd64.deb (or similar, depending on your architecture) in bazel-bin/pkg/.
Note: other supported binary packages (see details in Official build section)
:kong_deb:kong_el8:kong_aws2:kong_aws2023
7. Build a Custom Docker Image (kong-eureka)
To containerize your custom-compiled Kong Gateway using the .deb package, follow these steps.
Navigate to the Kong repository root:
cd kongFetch the docker-entrypoint.sh script:
This script is commonly used in official Kong Docker images to handle startup and configuration.curl -O https://raw.githubusercontent.com/Kong/docker-kong/master/docker-entrypoint.sh chmod +x docker-entrypoint.shMove the generated Debian package to the Docker build context:
mv bazel-bin/pkg/kong.amd64.deb .Create a Dockerfile:
Create a file namedDockerfilein the current directory (kong repository root) with the following content. This Dockerfile is based on the official Kong Docker builds.FROM ubuntu:24.04 COPY kong.amd64.deb /tmp/kong.deb RUN set -ex; \ apt-get update \ && apt-get install --yes /tmp/kong.deb \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/kong.deb \ && chown kong:0 /usr/local/bin/kong \ && chown -R kong:0 /usr/local/kong \ && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/luajit \ && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/lua \ && ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/bin/nginx \ && kong version COPY docker-entrypoint.sh /docker-entrypoint.sh USER kong ENTRYPOINT ["/docker-entrypoint.sh"] EXPOSE 8000 8443 8001 8444 8002 8445 8003 8446 8004 8447 STOPSIGNAL SIGQUIT HEALTHCHECK --interval=10s --timeout=10s --retries=10 CMD kong health CMD ["kong", "docker-start"]Build the Docker image:
Run the following command in the kong repository root (where yourDockerfile,docker-entrypoint.sh, andkong.amd64.debare located).docker build --platform linux/amd64 --no-cache -t kong-eureka .Run and verify the custom Docker image:
docker run -it --rm kong-eureka kong versionThis command will run your custom
kong-eurekaimage and executekong versioninside it, performing a sanity check of the successful build and packaging. The output will look like:
3.10.0
Running and testing Kong Gateway docker image
1. Run Kong with docker-compose
1.1 Create a docker-compose.yml file:
Place the following content into a file named docker-compose.yml in your chosen directory.
This docker-compose.yml file defines a multi-service application and based on docker-compose file from Install Kong Gateway using Docker Compose section of official Kong documentation:
kong-database: A PostgreSQL database container that Kong uses to store its configuration. It uses a named volume (
kong_db_data) for data persistence.kong-bootstrap: A temporary service that runs Kong database migrations to set up the schema in PostgreSQL.
kong-cp: The main Kong Control Plane service, which runs the Kong Gateway. It exposes various ports for the proxy (8000/8443), Admin API (8001/8444), and Kong Manager GUI (8002/8445). It waits for the
kong-bootstrapto complete successfully. It defaults to using thekong-eureka:latestimage.wiremock: A WireMock server, useful for API mocking during development or testing. It's connected to the same network as Kong and exposes port 8080. It also mounts a volume for persisting mappings.
kong-net: A custom bridge network for internal communication between services.
kong_db_data: A named volume for persistent PostgreSQL data.
1.2 Prepare for WireMock
Create a directory for its persistent data and mappings:
mkdir wiremock-data
and two sub-directories
__files– place in this directorymappings– place in this directory
1.3 Start the services with Docker Compose:
Ensure you are in the directory containing docker-compose.yml.
# To use your custom 'kong-eureka' image (which is the default in the provided docker-compose.yml)
docker compose up -d
# If you want to explicitly specify the image (e.g., if you renamed it or want to use a specific tag)
# GW_IMAGE=kong-eureka:latest docker compose up -d
2. Verify the installation
Verify Kong Gateway Admin API
curl -X GET "http://localhost:8001/"
You should receive a JSON response from Kong's Admin API:
Configure test Service and Route in Access Kong Manager (GUI):
Open web browser and go to http://localhost:8002. You should see the Kong Manager main page:
Go to Gateway Service and create a test Service:
Configure a new Route associated with test-api Service:
Verify Kong routes calls to Wiremock:
curl -X GET "http://localhost:8000/test/api/data" | jq .
The output should be:
{
"message": "Hello from WireMock!"
}