kamal icon indicating copy to clipboard operation
kamal copied to clipboard

Enable per‑service Docker build contexts in config/deploy.yml for microservices based monorepo

Open aphronio opened this issue 7 months ago • 1 comments

I’d like to maintain separate Dockerfiles (e.g. one for Next.js, one for FastAPI/Celery) and have Kamal build and push each automatically on deploy. Right now Kamal only supports a single global builder: section, and rejects any per‑role build: keys under servers::

service: myapp

registry:
  server: registry.example.com
  username: myuser
  password:
    - REGISTRY_TOKEN

# Only one global build is supported today
builder:
  context: .
  dockerfile: Dockerfile

servers:
  web:
    hosts: [1.2.3.4]
    # ❌ unknown key: build
    build:
      context: ./frontend
      dockerfile: Dockerfile
    expose: "3000"

  api:
    hosts: [1.2.3.4]
    # ❌ unknown key: build
    build:
      context: ./backend
      dockerfile: Dockerfile
    expose: "8000"

What I want to achieve:

  • Per‑role build contexts so each service can point at its own Dockerfile and directory.
  • Kamal handles all builds and pushes, then rolls out zero‑downtime updates for each role.

Limitation:

  • Current schema only allows one top‑level builder: and no build: under servers:.

Questions:

  1. Is this a deliberate design choice (with some benefits)?
  2. If not, could per‑role build: blocks be supported (or could the global builder: accept multiple named targets)?

Thanks for any guidance or roadmap on this!

aphronio avatar May 05 '25 17:05 aphronio

I use different configuration files to achieve something similar.

deploy_api.yml:

service: myapp

registry:
   ...

builder:
  context: ./backend
  dockerfile: Dockerfile

servers:
  api:
    hosts: [1.2.3.4]
    expose: "8000"

deploy_web.yml:

service: myapp

registry:
   ...

builder:
  context: ./frontend
  dockerfile: Dockerfile

servers:
  web:
    hosts: [1.2.3.4]
    expose: "3000"

Provide config -c when calling kamal deploy:

kamal deploy -c config/deploy_api.yml 
kamal deploy -c config/deploy_web.yml 

This also supports destinations automatically (e.g. if -d web provided, it uses deploy_web.dev.yml config).

ahaltindis avatar Jul 05 '25 00:07 ahaltindis