DEV Community

Sahithi V
Sahithi V

Posted on

How to Convert a docker-compose file to K8S manifest

How to Convert a Docker Compose File to Kubernetes Manifests Using Kompose

Many of us use Docker for local development, but when it’s time to run containers on Kubernetes, converting your setup can seem daunting. Kompose makes this transition easy by converting your docker-compose.yml into Kubernetes manifests.

Below are the latest steps to get your services running on Kubernetes:


1. Install Kompose (Latest Version)

On Mac (using Homebrew):

brew install kompose
Enter fullscreen mode Exit fullscreen mode

Or download the latest release manually:

curl -L https://github.com/kubernetes/kompose/releases/latest/download/kompose-darwin-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose
Enter fullscreen mode Exit fullscreen mode

2. Prepare Your Docker Compose File

  • Build and push your Docker images to a remote registry.
  • Update your docker-compose.yml so that each service uses the image URL from your registry (not a local build context).

Example docker-compose.yml:

version: "3"
services:
  mythical-requester:
    image: <REMOTE-IMAGE>
    ports:
      - "4001:4001"
    environment:
      - NAMESPACE=production
      - LOGS_TARGET=http://loki:3100/loki/api/v1/push
      - TRACING_COLLECTOR_HOST=agent
      - TRACING_COLLECTOR_PORT=4317
      - OTEL_EXPORTER_OTLP_TRACES_INSECURE=true
      - OTEL_RESOURCE_ATTRIBUTES=ip=1.2.3.4

  mythical-server:
    image: <REMOTE-IMAGE>
    ports:
      - "4000:4000"
      - "80:80"
    depends_on:
      - mythical-database
    environment:
      - NAMESPACE=production
      - LOGS_TARGET=http://loki:3100/loki/api/v1/push
      - TRACING_COLLECTOR_HOST=agent
      - TRACING_COLLECTOR_PORT=4317
      - OTEL_EXPORTER_OTLP_TRACES_INSECURE=true
      - OTEL_RESOURCE_ATTRIBUTES=ip=1.2.3.5

  mythical-recorder:
    image: <REMOTE-IMAGE>
    ports:
      - "4002:4002"
    environment:
      - NAMESPACE=production
      - LOGS_TARGET=http://loki:3100/loki/api/v1/push
      - TRACING_COLLECTOR_HOST=agent
      - TRACING_COLLECTOR_PORT=4317
      - OTEL_EXPORTER_OTLP_TRACES_INSECURE=true
      - OTEL_RESOURCE_ATTRIBUTES=ip=1.2.3.5
Enter fullscreen mode Exit fullscreen mode

3. Convert Docker Compose to Kubernetes Manifests

Run the following command in the directory containing your docker-compose.yml:

kompose convert
Enter fullscreen mode Exit fullscreen mode

Kompose will generate Kubernetes YAML files for each service, typically creating both a deployment and a service manifest per service.


4. Deploy to Kubernetes

  1. Move all generated YAML files to a folder (e.g., deployment/):

    mkdir deployment
    mv *.yaml deployment/
    
  2. Connect to your Kubernetes cluster (ensure kubectl is configured).

  3. Apply all manifests:

    kubectl apply -f deployment/
    
  4. Check your pods:

    kubectl get pods
    

Congratulations!

Your Docker Compose services are now running on your Kubernetes cluster.


References:

Top comments (0)