
Portkey is an open-source AI gateway that unifies access to 250+ Large Language Models (LLMs) across dozens of providers behind a single, OpenAI-compatible API. Instead of integrating with each provider's SDK, applications send requests through Portkey, which handles routing, load balancing, and automatic fallback to keep AI-powered systems reliable.
This article explains how to deploy Portkey on a Linux server using Docker Compose and secure it with Traefik as a reverse proxy for TLS termination. It also covers protecting access with a combination of Basic Authentication for the web console and an IP allowlist for the API endpoint.
Before you begin, you need to:
portkey.example.com, pointing to your server's IP address.Portkey's gateway container is stateless and does not require persistent storage of its own. The project directory contains the Docker Compose configuration and environment variables used to configure and run the gateway.
Create the project directory.
Navigate to the project directory.
Find your server's public IP address, which is used later to restrict API access.
Copy this value for use when configuring the .env file.
Create an environment file to store configuration variables.
Add the following configuration.
portkey.example.com with your domain name.admin@example.com with your email address.YOUR_SERVER_IP with your server's public IP address obtained in the earlier step.Save and close the file.
Install the htpasswd utility, which is used to generate credentials for the web console.
Generate a username and password for accessing the Portkey console. Replace portkeyadmin with your preferred username.
Enter and confirm a password when prompted. This creates a .htpasswd file containing the hashed credentials.
This section deploys Portkey and Traefik together, with Traefik handling HTTPS certificate issuance through Let's Encrypt. The self-hosted Portkey gateway has no built-in authentication of its own, so this guide adds Basic Authentication and an IP allowlist at the Traefik level to restrict access to the console and the API.
Create the Docker Compose manifest file.
Add the following configuration.
Save and close the file.
In the above manifest:
traefik: Serves as the reverse proxy and TLS termination point. It listens on ports 80 and 443, automatically redirects HTTP to HTTPS, and provisions Let's Encrypt certificates.portkey: Runs the official portkeyai/gateway image on internal port 8787. Separate Traefik routers apply Basic Authentication to the /public console and an IP allowlist to the API, avoiding conflicts with provider Authorization headers. The stateless gateway requires no environment variables, volumes, or database.Start the services in detached mode.
Verify that all containers are running.
The output displays two running containers with Traefik listening on ports 80 and 443.
For more information on managing a Docker Compose stack, see the How to Use Docker Compose article.
Traefik routes all HTTPS traffic on the configured domain to the Portkey container, applying Basic Authentication to the console and an IP allowlist to the API.
Confirm that the API responds from your server. Replace portkey.example.com with your configured domain.
A response other than 403 confirms that the request reached the gateway and was not blocked by the IP allowlist. A 403 response means the server's IP does not match the value configured in SERVER_IP in the .env file.
Open a web browser and navigate to https://portkey.example.com/public/, replacing portkey.example.com with your configured domain.
A browser prompt requests a username and password. Enter the credentials created earlier with htpasswd.
The Portkey Console displays a test request panel and options to configure routing rules such as load balancing, fallbacks, and retries.
Validate the deployment by sending a live chat completion request through the gateway using an LLM provider API key.
Send a test chat completion request through the gateway. Replace portkey.example.com with your configured domain, your_provider with your LLM provider, and your_api_key and your_model with your provider's key and model name.
A successful response returns a chat.completion object containing the model's reply, confirming that the gateway routed the request to the provider and back.
Open the Portkey Console at https://portkey.example.com/public/, replacing portkey.example.com with your configured domain and review the request under the Logs section, confirming the gateway recorded the completed request.
You have deployed Portkey on a Linux server using Docker Compose, with Traefik as a reverse proxy for TLS termination. The deployment routes requests to language model providers through a single, OpenAI-compatible endpoint, with the web console protected by Basic Authentication and the API restricted through an IP allowlist. For more information, see the official Portkey documentation.
0 Comments
Be the first to comment and share your perspective with the community.