<p>How can I replace the Image used in a Kubernetes Deployment manifest with jq?</p>
<p>For example:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: myapp-deployment
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: myapp
        image: myapp:v1
</code></pre>
<p>I tried using something like this <code>jq '.spec.template.spec.containers[0].image = &quot;myapp:v2&quot;'</code>. However, it always ends with a syntax or parse error.</p>

Frederik
<p>Using <a href="https://kislyuk.github.io/yq/" rel="nofollow noreferrer"><code>yq</code></a>, you can simply write:</p>
<pre><code>yq -y '.spec.template.spec.containers[0].image = &quot;foo:latest&quot;' pod.yml
</code></pre>
<p>Which produces:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: myapp-deployment
spec:
  replicas: 1
  template:
    spec:
      containers:
        - name: myapp
          image: foo:latest
</code></pre>
<p>But I would use <a href="https://kustomize.io/" rel="nofollow noreferrer">kustomize</a> for something like this, as @DavidMaze suggested.</p>
