Convert Any Docker-Compose YAML to Kubernetes Manifests in Seconds with Kompose
Convert Docker-Compose to Kubernetes in Seconds
Introduction
If you are using Docker Compose and want to switch to Kubernetes, you may find it difficult to manually write Kubernetes YAML files.
Kompose is a tool that helps convert any docker-compose.yaml
file into Kubernetes YAML files with a single command.
In this guide, I will show how to:
- Run a FastAPI application with Docker Compose
- Convert it to Kubernetes using Kompose
- Deploy it on Minikube
1. Install Kompose and Minikube
Before starting, install Kompose and Minikube.
Install Kompose
Follow the official installation guide: Kompose Installation
For Linux/macOS:
$ curl -L https://github.com/kubernetes/kompose/releases/latest/download/kompose-linux-amd64 -o kompose
$ chmod +x kompose
$ sudo mv kompose /usr/local/bin/
For Windows:
Download the latest release from Kompose Releases and add it to your system’s PATH.
Verify installation by running:
$ kompose version
Install Minikube
Follow the instructions on the Minikube website to install it.
Start Minikube:
$ minikube start
2. Setup FastAPI Project
The FastAPI application is structured as follows:
kompose-fastapi/
│── Dockerfile
│── docker-compose.yml
│── pyproject.toml
│── src/
│ ├── app/
│ │ ├── __init__.py
│ │ ├── main.py
│ ├── config/
│ │ ├── __init__.py
│ │ ├── settings.py
│── k8s/ (Generated after Kompose conversion)
3. FastAPI Application Code
Create a FastAPI application inside the src/app/main.py
file:
from fastapi import FastAPI
from src.config.settings import settings
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
description=settings.app_description,
)
@app.get("/")
async def root():
return {
"app_name": settings.app_name,
"version": settings.app_version,
"environment": settings.app_env,
"debug": settings.debug,
}
Configuration is stored in src/config/settings.py
:
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
app_name: str = "Kompose FastAPI"
app_version: str = "1.0.0"
app_description: str = "A FastAPI service for Kompose testing"
app_env: str = "development"
debug: bool = False
api_prefix: str = "/api/v1"
class Config:
env_file = ".env"
case_sensitive = False
settings = Settings()
4. Docker Setup
Create a Dockerfile
to containerize the application:
FROM python:3.12-slim
WORKDIR /app
RUN pip install poetry
COPY pyproject.toml ./
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi
COPY src/ ./src/
EXPOSE 8000
CMD ["poetry", "run", "uvicorn", "src.app.main:app", "--host", "0.0.0.0", "--port", "8000"]
5. Build and Push Docker Image to Docker Hub
Step 1: Build the Docker Image
Run the following command to build the FastAPI Docker image:
$ docker build -t anqorithm/kompose-fastapi:latest .
Step 2: Log in to Docker Hub
Authenticate with Docker Hub before pushing the image:
$ docker login
Step 3: Push the Image to Docker Hub
Upload the image to Docker Hub using:
$ docker push anqorithm/kompose-fastapi:latest
Step 4: Verify the Image on Docker Hub
Check that the image is available on Docker Hub by running:
$ docker images | grep anqorithm/kompose-fastapi
Alternatively, visit Docker Hub and search for anqorithm/kompose-fastapi
.
Step 5: Use the Image in Kubernetes
Now, the image is stored on Docker Hub and can be referenced in the Kubernetes deployment YAML as:
containers:
- name: api
image: anqorithm/kompose-fastapi:latest
ports:
- containerPort: 8000
6. Docker Compose Setup
Create a docker-compose.yml
file:
services:
api:
build: .
ports:
- "8000:8000"
environment:
- APP_NAME=Kompose FastAPI
- APP_VERSION=1.0.0
- APP_DESCRIPTION=FastAPI Application for Kompose Testing
- APP_ENV=development
- DEBUG=true
- API_PREFIX=/api/v1
volumes:
- ./src:/app/src
Run the application with Docker Compose:
$ docker-compose up -d
Test if it is running:
$ curl http://localhost:8000
7. Convert to Kubernetes with Kompose
Run the conversion command:
$ kompose convert -f docker-compose.yml
This generates Kubernetes YAML files in the k8s/
directory, including:
api-deployment.yaml
api-service.yaml
Example: Kubernetes Deployment File
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.35.0 (HEAD)
labels:
io.kompose.service: api
name: api
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: api
strategy:
type: Recreate
template:
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.35.0 (HEAD)
labels:
io.kompose.service: api
spec:
containers:
- env:
- name: API_PREFIX
value: /api/v1
- name: APP_DESCRIPTION
value: Kompose FastAPI
- name: APP_ENV
value: development
- name: APP_NAME
value: Kompose FastAPI
- name: APP_VERSION
value: 1.0.0
- name: DEBUG
value: "true"
image: anqorithm/kompose-fastapi:latest
name: api
ports:
- containerPort: 8000
protocol: TCP
restartPolicy: Always
Example: Kubernetes Service File
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.35.0 (HEAD)
labels:
io.kompose.service: api
name: api
spec:
ports:
- name: "8000"
port: 8000
targetPort: 8000
selector:
io.kompose.service: api
Note: I will move them to k8s directory to make it more organized.
8. Deploy to Minikube
Apply the Kubernetes files to Minikube:
$ kubectl apply -f k8s/
Check the running pods:
$ kubectl get pods
Check the logs of the pod:
$ kubectl logs api-6854688558-2ntgv
Check the services:
$ kubectl get svc
Expose the service in Minikube:
$ minikube service api
9. Conclusion
- Kompose helps convert
docker-compose.yml
to Kubernetes YAML easily. - This method allows moving FastAPI applications (or any dockerized application) from Docker Compose to Kubernetes quickly.
- Minikube provides a local Kubernetes environment for testing.
This is a simple way to start using Kubernetes without writing YAML files manually.
10. Source Code in GitHub
The source code used in this article can be found here in Github:
11. References
- Kompose Documentation: Installation Guide, Usage Guide
- Minikube Documentation: Getting Started with Minikube
- Docker Hub: Docker Hub — anqorithm/kompose-fastapi
- FastAPI Documentation: FastAPI Framework
- Kubernetes Documentation: Kubernetes Official Docs