patchesJSON6902

• For similar examples, refer to the Kustomize documentation on patchesJSON6902.

• Example files are available in the Kompozit GitHub repository.

Kompozit supports JSON Patch (RFC 6902) to modify resources in a flexible way. The jsonpatch module is used internally in Kompozit to apply standard operations such as:

  • add: Adds a value to the specified path.

  • remove: Removes the value at the specified path.

  • replace: Replaces the value at the specified path.

This allows you to precisely control how resources are modified in your configurations.

For example, to replace the Docker image for the Traefik service, you can define the following patch in the kompozition file:

```yaml
---
resources:
  - ../../base

patchesJSON6902:
  - patch:
      - op: replace
        path: /services/traefik/image
        value: "traefik:v2"
```

Parameters:

  • op: The operation you want to perform. In this case, it’s replace.

  • path: The JSON path where you want to apply the operation.(Here, it’s /services/traefik/image).

  • value: The value to be used in the replace operation, such as "traefik:v2".

Result After Applying the Patch

$ kompozit -b overlay/homeserver/
---
networks:
  public:
    attachable: true
    internal: false
  private:
    attachable: true
    internal: true
volumes:
  acme: null
services:
  traefik:
    image: traefik:v2 #<---- Docker image replaced
    hostname: traefik
    container_name: traefik
    restart: unless-stopped
    environment:
      DUCKDNS_TOKEN: ${DUCKDNS_TOKEN}
      CLOUDFLARE_DNS_API_TOKEN: ${CLOUDFLARE_DNS_API_TOKEN}
    ports:
    - 80:80/tcp
    - 443:443/tcp
    networks:
    - public
    - private
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - acme:/letsencrypt

Caveat

The patches defined in the kompozition file will be applied to all matching JSON paths across all resources. For example, if multiple docker-compose.yaml files define the same service name (e.g., traefik), the patch will be applied to all instances of that service, even across different files.

Last updated