Skip to main content
Version: 6.x (Latest)

Custom Patches To Dev Containers' Pods

Besides the common shortcut modifications such as setting devImage, DevSpace also allows you to write custom patches to the Kubernetes PodSpec when a pod is being replaced to turn one (or multiple) of its containers into a dev container.

Custom Patches

For advanced use cases, DevSpace provides a feature for custom patches to the Kubernetes PodSpec when a pod is being replaced. To add custom patches, add them to the patches field.

devspace.yaml
dev:
backend:
imageSelector: ghcr.io/org/project/image
devImage: ghcr.io/loft-sh/devspace-containers/python:3-alpine
patches:
- op: replace
path: spec.terminationGracePeriodSeconds
value: 3
- op: remove
path: spec.securityContext
- op: add
path: spec.containers.name=backend.command
value: ""
Patch Specification

Patches in DevSpace generally follow the JSON Patch standard (RFC 9602) as implemented by the yaml-jsonpath library.

add Operation

Using op: add only works as expected when path points to an array/list. Using op: add to add fields to an object/map will not work and instead replace all existing properties.

path + Arrays/Lists

When you want to define a path that contains an array (e.g. spec.containers), you have several options:

  1. Use the index of the array item you want to patch, e.g. spec.containers[0]
  2. Use a property selector matching the array item(s) you want to patch, e.g. spec.containers.name=backend
  3. Use a wildcard selector to match all array item(s), e.g. spec.containers.*
  4. Use a comparison selector matching the array item(s) you want to patch, e.g. spec.containers[?(@.image=='ghcr.io/loft-sh/image')]
  5. Use a regular expression selector matching the array item(s) you want to patch, e.g. spec.containers[?(@.name=~/^production/)]

Using a selector rather than the array index is often better because it is more resilient and will not cause any issues even if the order of an array's items is changed later on. A selector is also able to select multiple array items if all of them have the same value for this property.

Implicit Patches

DevSpace adds certain modifications and patches in-memory if certain conditions are met. This helps to keep devspace.yaml shorter and makes its configuration easier. The following table shows a list of implicit modifications and patches added by DevSpace:

ConditionImplicit Configuration

terminal enabled or
attach enabled

devspace.yaml
dev:
$name:
command: ["sleep"]
args: ["1000000000"]
patches:
- op: remove
path: spec.containers[].livenessProbe
- op: remove
path: spec.containers[].readinessProbe
- op: remove
path: spec.containers[].startupProbe

sync enabled

devspace.yaml
dev:
$name:
command: ["sleep"]
args: ["1000000000"]
patches:
- op: remove
path: spec.containers[].securityContext.readOnlyRootFilesystem

Config Reference

patches required object[]

Patches are additional changes to the pod spec that should be applied

op required string

Operation is the path operation to do. Can be either replace, add or remove

path required string

Path is the config path to apply the patch to

value required

Value is the value to use for this patch.